hid-picolcd.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  1. /***************************************************************************
  2. * Copyright (C) 2010 by Bruno Prémont <bonbons@linux-vserver.org> *
  3. * *
  4. * Based on Logitech G13 driver (v0.4) *
  5. * Copyright (C) 2009 by Rick L. Vinyard, Jr. <rvinyard@cs.nmsu.edu> *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, version 2 of the License. *
  10. * *
  11. * This driver is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this software. If not see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #include <linux/hid.h>
  20. #include <linux/hid-debug.h>
  21. #include <linux/input.h>
  22. #include "hid-ids.h"
  23. #include "usbhid/usbhid.h"
  24. #include <linux/usb.h>
  25. #include <linux/fb.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/backlight.h>
  28. #include <linux/lcd.h>
  29. #include <linux/leds.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/completion.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/module.h>
  35. #define PICOLCD_NAME "PicoLCD (graphic)"
  36. /* Report numbers */
  37. #define REPORT_ERROR_CODE 0x10 /* LCD: IN[16] */
  38. #define ERR_SUCCESS 0x00
  39. #define ERR_PARAMETER_MISSING 0x01
  40. #define ERR_DATA_MISSING 0x02
  41. #define ERR_BLOCK_READ_ONLY 0x03
  42. #define ERR_BLOCK_NOT_ERASABLE 0x04
  43. #define ERR_BLOCK_TOO_BIG 0x05
  44. #define ERR_SECTION_OVERFLOW 0x06
  45. #define ERR_INVALID_CMD_LEN 0x07
  46. #define ERR_INVALID_DATA_LEN 0x08
  47. #define REPORT_KEY_STATE 0x11 /* LCD: IN[2] */
  48. #define REPORT_IR_DATA 0x21 /* LCD: IN[63] */
  49. #define REPORT_EE_DATA 0x32 /* LCD: IN[63] */
  50. #define REPORT_MEMORY 0x41 /* LCD: IN[63] */
  51. #define REPORT_LED_STATE 0x81 /* LCD: OUT[1] */
  52. #define REPORT_BRIGHTNESS 0x91 /* LCD: OUT[1] */
  53. #define REPORT_CONTRAST 0x92 /* LCD: OUT[1] */
  54. #define REPORT_RESET 0x93 /* LCD: OUT[2] */
  55. #define REPORT_LCD_CMD 0x94 /* LCD: OUT[63] */
  56. #define REPORT_LCD_DATA 0x95 /* LCD: OUT[63] */
  57. #define REPORT_LCD_CMD_DATA 0x96 /* LCD: OUT[63] */
  58. #define REPORT_EE_READ 0xa3 /* LCD: OUT[63] */
  59. #define REPORT_EE_WRITE 0xa4 /* LCD: OUT[63] */
  60. #define REPORT_ERASE_MEMORY 0xb2 /* LCD: OUT[2] */
  61. #define REPORT_READ_MEMORY 0xb3 /* LCD: OUT[3] */
  62. #define REPORT_WRITE_MEMORY 0xb4 /* LCD: OUT[63] */
  63. #define REPORT_SPLASH_RESTART 0xc1 /* LCD: OUT[1] */
  64. #define REPORT_EXIT_KEYBOARD 0xef /* LCD: OUT[2] */
  65. #define REPORT_VERSION 0xf1 /* LCD: IN[2],OUT[1] Bootloader: IN[2],OUT[1] */
  66. #define REPORT_BL_ERASE_MEMORY 0xf2 /* Bootloader: IN[36],OUT[4] */
  67. #define REPORT_BL_READ_MEMORY 0xf3 /* Bootloader: IN[36],OUT[4] */
  68. #define REPORT_BL_WRITE_MEMORY 0xf4 /* Bootloader: IN[36],OUT[36] */
  69. #define REPORT_DEVID 0xf5 /* LCD: IN[5], OUT[1] Bootloader: IN[5],OUT[1] */
  70. #define REPORT_SPLASH_SIZE 0xf6 /* LCD: IN[4], OUT[1] */
  71. #define REPORT_HOOK_VERSION 0xf7 /* LCD: IN[2], OUT[1] */
  72. #define REPORT_EXIT_FLASHER 0xff /* Bootloader: OUT[2] */
  73. #ifdef CONFIG_HID_PICOLCD_FB
  74. /* Framebuffer
  75. *
  76. * The PicoLCD use a Topway LCD module of 256x64 pixel
  77. * This display area is tiled over 4 controllers with 8 tiles
  78. * each. Each tile has 8x64 pixel, each data byte representing
  79. * a 1-bit wide vertical line of the tile.
  80. *
  81. * The display can be updated at a tile granularity.
  82. *
  83. * Chip 1 Chip 2 Chip 3 Chip 4
  84. * +----------------+----------------+----------------+----------------+
  85. * | Tile 1 | Tile 1 | Tile 1 | Tile 1 |
  86. * +----------------+----------------+----------------+----------------+
  87. * | Tile 2 | Tile 2 | Tile 2 | Tile 2 |
  88. * +----------------+----------------+----------------+----------------+
  89. * ...
  90. * +----------------+----------------+----------------+----------------+
  91. * | Tile 8 | Tile 8 | Tile 8 | Tile 8 |
  92. * +----------------+----------------+----------------+----------------+
  93. */
  94. #define PICOLCDFB_NAME "picolcdfb"
  95. #define PICOLCDFB_WIDTH (256)
  96. #define PICOLCDFB_HEIGHT (64)
  97. #define PICOLCDFB_SIZE (PICOLCDFB_WIDTH * PICOLCDFB_HEIGHT / 8)
  98. #define PICOLCDFB_UPDATE_RATE_LIMIT 10
  99. #define PICOLCDFB_UPDATE_RATE_DEFAULT 2
  100. /* Framebuffer visual structures */
  101. static const struct fb_fix_screeninfo picolcdfb_fix = {
  102. .id = PICOLCDFB_NAME,
  103. .type = FB_TYPE_PACKED_PIXELS,
  104. .visual = FB_VISUAL_MONO01,
  105. .xpanstep = 0,
  106. .ypanstep = 0,
  107. .ywrapstep = 0,
  108. .line_length = PICOLCDFB_WIDTH / 8,
  109. .accel = FB_ACCEL_NONE,
  110. };
  111. static const struct fb_var_screeninfo picolcdfb_var = {
  112. .xres = PICOLCDFB_WIDTH,
  113. .yres = PICOLCDFB_HEIGHT,
  114. .xres_virtual = PICOLCDFB_WIDTH,
  115. .yres_virtual = PICOLCDFB_HEIGHT,
  116. .width = 103,
  117. .height = 26,
  118. .bits_per_pixel = 1,
  119. .grayscale = 1,
  120. .red = {
  121. .offset = 0,
  122. .length = 1,
  123. .msb_right = 0,
  124. },
  125. .green = {
  126. .offset = 0,
  127. .length = 1,
  128. .msb_right = 0,
  129. },
  130. .blue = {
  131. .offset = 0,
  132. .length = 1,
  133. .msb_right = 0,
  134. },
  135. .transp = {
  136. .offset = 0,
  137. .length = 0,
  138. .msb_right = 0,
  139. },
  140. };
  141. #endif /* CONFIG_HID_PICOLCD_FB */
  142. /* Input device
  143. *
  144. * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys
  145. * and header for 4x4 key matrix. The built-in keys are part of the matrix.
  146. */
  147. static const unsigned short def_keymap[] = {
  148. KEY_RESERVED, /* none */
  149. KEY_BACK, /* col 4 + row 1 */
  150. KEY_HOMEPAGE, /* col 3 + row 1 */
  151. KEY_RESERVED, /* col 2 + row 1 */
  152. KEY_RESERVED, /* col 1 + row 1 */
  153. KEY_SCROLLUP, /* col 4 + row 2 */
  154. KEY_OK, /* col 3 + row 2 */
  155. KEY_SCROLLDOWN, /* col 2 + row 2 */
  156. KEY_RESERVED, /* col 1 + row 2 */
  157. KEY_RESERVED, /* col 4 + row 3 */
  158. KEY_RESERVED, /* col 3 + row 3 */
  159. KEY_RESERVED, /* col 2 + row 3 */
  160. KEY_RESERVED, /* col 1 + row 3 */
  161. KEY_RESERVED, /* col 4 + row 4 */
  162. KEY_RESERVED, /* col 3 + row 4 */
  163. KEY_RESERVED, /* col 2 + row 4 */
  164. KEY_RESERVED, /* col 1 + row 4 */
  165. };
  166. #define PICOLCD_KEYS ARRAY_SIZE(def_keymap)
  167. /* Description of in-progress IO operation, used for operations
  168. * that trigger response from device */
  169. struct picolcd_pending {
  170. struct hid_report *out_report;
  171. struct hid_report *in_report;
  172. struct completion ready;
  173. int raw_size;
  174. u8 raw_data[64];
  175. };
  176. /* Per device data structure */
  177. struct picolcd_data {
  178. struct hid_device *hdev;
  179. #ifdef CONFIG_DEBUG_FS
  180. struct dentry *debug_reset;
  181. struct dentry *debug_eeprom;
  182. struct dentry *debug_flash;
  183. struct mutex mutex_flash;
  184. int addr_sz;
  185. #endif
  186. u8 version[2];
  187. unsigned short opmode_delay;
  188. /* input stuff */
  189. u8 pressed_keys[2];
  190. struct input_dev *input_keys;
  191. struct input_dev *input_cir;
  192. unsigned short keycode[PICOLCD_KEYS];
  193. #ifdef CONFIG_HID_PICOLCD_FB
  194. /* Framebuffer stuff */
  195. u8 fb_update_rate;
  196. u8 fb_bpp;
  197. u8 fb_force;
  198. u8 *fb_vbitmap; /* local copy of what was sent to PicoLCD */
  199. u8 *fb_bitmap; /* framebuffer */
  200. struct fb_info *fb_info;
  201. struct fb_deferred_io fb_defio;
  202. #endif /* CONFIG_HID_PICOLCD_FB */
  203. #ifdef CONFIG_HID_PICOLCD_LCD
  204. struct lcd_device *lcd;
  205. u8 lcd_contrast;
  206. #endif /* CONFIG_HID_PICOLCD_LCD */
  207. #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
  208. struct backlight_device *backlight;
  209. u8 lcd_brightness;
  210. u8 lcd_power;
  211. #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
  212. #ifdef CONFIG_HID_PICOLCD_LEDS
  213. /* LED stuff */
  214. u8 led_state;
  215. struct led_classdev *led[8];
  216. #endif /* CONFIG_HID_PICOLCD_LEDS */
  217. /* Housekeeping stuff */
  218. spinlock_t lock;
  219. struct mutex mutex;
  220. struct picolcd_pending *pending;
  221. int status;
  222. #define PICOLCD_BOOTLOADER 1
  223. #define PICOLCD_FAILED 2
  224. #define PICOLCD_READY_FB 4
  225. };
  226. /* Find a given report */
  227. #define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT)
  228. #define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT)
  229. static struct hid_report *picolcd_report(int id, struct hid_device *hdev, int dir)
  230. {
  231. struct list_head *feature_report_list = &hdev->report_enum[dir].report_list;
  232. struct hid_report *report = NULL;
  233. list_for_each_entry(report, feature_report_list, list) {
  234. if (report->id == id)
  235. return report;
  236. }
  237. hid_warn(hdev, "No report with id 0x%x found\n", id);
  238. return NULL;
  239. }
  240. #ifdef CONFIG_DEBUG_FS
  241. static void picolcd_debug_out_report(struct picolcd_data *data,
  242. struct hid_device *hdev, struct hid_report *report);
  243. #define usbhid_submit_report(a, b, c) \
  244. do { \
  245. picolcd_debug_out_report(hid_get_drvdata(a), a, b); \
  246. usbhid_submit_report(a, b, c); \
  247. } while (0)
  248. #endif
  249. /* Submit a report and wait for a reply from device - if device fades away
  250. * or does not respond in time, return NULL */
  251. static struct picolcd_pending *picolcd_send_and_wait(struct hid_device *hdev,
  252. int report_id, const u8 *raw_data, int size)
  253. {
  254. struct picolcd_data *data = hid_get_drvdata(hdev);
  255. struct picolcd_pending *work;
  256. struct hid_report *report = picolcd_out_report(report_id, hdev);
  257. unsigned long flags;
  258. int i, j, k;
  259. if (!report || !data)
  260. return NULL;
  261. if (data->status & PICOLCD_FAILED)
  262. return NULL;
  263. work = kzalloc(sizeof(*work), GFP_KERNEL);
  264. if (!work)
  265. return NULL;
  266. init_completion(&work->ready);
  267. work->out_report = report;
  268. work->in_report = NULL;
  269. work->raw_size = 0;
  270. mutex_lock(&data->mutex);
  271. spin_lock_irqsave(&data->lock, flags);
  272. for (i = k = 0; i < report->maxfield; i++)
  273. for (j = 0; j < report->field[i]->report_count; j++) {
  274. hid_set_field(report->field[i], j, k < size ? raw_data[k] : 0);
  275. k++;
  276. }
  277. data->pending = work;
  278. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  279. spin_unlock_irqrestore(&data->lock, flags);
  280. wait_for_completion_interruptible_timeout(&work->ready, HZ*2);
  281. spin_lock_irqsave(&data->lock, flags);
  282. data->pending = NULL;
  283. spin_unlock_irqrestore(&data->lock, flags);
  284. mutex_unlock(&data->mutex);
  285. return work;
  286. }
  287. #ifdef CONFIG_HID_PICOLCD_FB
  288. /* Send a given tile to PicoLCD */
  289. static int picolcd_fb_send_tile(struct hid_device *hdev, int chip, int tile)
  290. {
  291. struct picolcd_data *data = hid_get_drvdata(hdev);
  292. struct hid_report *report1 = picolcd_out_report(REPORT_LCD_CMD_DATA, hdev);
  293. struct hid_report *report2 = picolcd_out_report(REPORT_LCD_DATA, hdev);
  294. unsigned long flags;
  295. u8 *tdata;
  296. int i;
  297. if (!report1 || report1->maxfield != 1 || !report2 || report2->maxfield != 1)
  298. return -ENODEV;
  299. spin_lock_irqsave(&data->lock, flags);
  300. hid_set_field(report1->field[0], 0, chip << 2);
  301. hid_set_field(report1->field[0], 1, 0x02);
  302. hid_set_field(report1->field[0], 2, 0x00);
  303. hid_set_field(report1->field[0], 3, 0x00);
  304. hid_set_field(report1->field[0], 4, 0xb8 | tile);
  305. hid_set_field(report1->field[0], 5, 0x00);
  306. hid_set_field(report1->field[0], 6, 0x00);
  307. hid_set_field(report1->field[0], 7, 0x40);
  308. hid_set_field(report1->field[0], 8, 0x00);
  309. hid_set_field(report1->field[0], 9, 0x00);
  310. hid_set_field(report1->field[0], 10, 32);
  311. hid_set_field(report2->field[0], 0, (chip << 2) | 0x01);
  312. hid_set_field(report2->field[0], 1, 0x00);
  313. hid_set_field(report2->field[0], 2, 0x00);
  314. hid_set_field(report2->field[0], 3, 32);
  315. tdata = data->fb_vbitmap + (tile * 4 + chip) * 64;
  316. for (i = 0; i < 64; i++)
  317. if (i < 32)
  318. hid_set_field(report1->field[0], 11 + i, tdata[i]);
  319. else
  320. hid_set_field(report2->field[0], 4 + i - 32, tdata[i]);
  321. usbhid_submit_report(data->hdev, report1, USB_DIR_OUT);
  322. usbhid_submit_report(data->hdev, report2, USB_DIR_OUT);
  323. spin_unlock_irqrestore(&data->lock, flags);
  324. return 0;
  325. }
  326. /* Translate a single tile*/
  327. static int picolcd_fb_update_tile(u8 *vbitmap, const u8 *bitmap, int bpp,
  328. int chip, int tile)
  329. {
  330. int i, b, changed = 0;
  331. u8 tdata[64];
  332. u8 *vdata = vbitmap + (tile * 4 + chip) * 64;
  333. if (bpp == 1) {
  334. for (b = 7; b >= 0; b--) {
  335. const u8 *bdata = bitmap + tile * 256 + chip * 8 + b * 32;
  336. for (i = 0; i < 64; i++) {
  337. tdata[i] <<= 1;
  338. tdata[i] |= (bdata[i/8] >> (i % 8)) & 0x01;
  339. }
  340. }
  341. } else if (bpp == 8) {
  342. for (b = 7; b >= 0; b--) {
  343. const u8 *bdata = bitmap + (tile * 256 + chip * 8 + b * 32) * 8;
  344. for (i = 0; i < 64; i++) {
  345. tdata[i] <<= 1;
  346. tdata[i] |= (bdata[i] & 0x80) ? 0x01 : 0x00;
  347. }
  348. }
  349. } else {
  350. /* Oops, we should never get here! */
  351. WARN_ON(1);
  352. return 0;
  353. }
  354. for (i = 0; i < 64; i++)
  355. if (tdata[i] != vdata[i]) {
  356. changed = 1;
  357. vdata[i] = tdata[i];
  358. }
  359. return changed;
  360. }
  361. /* Reconfigure LCD display */
  362. static int picolcd_fb_reset(struct picolcd_data *data, int clear)
  363. {
  364. struct hid_report *report = picolcd_out_report(REPORT_LCD_CMD, data->hdev);
  365. int i, j;
  366. unsigned long flags;
  367. static const u8 mapcmd[8] = { 0x00, 0x02, 0x00, 0x64, 0x3f, 0x00, 0x64, 0xc0 };
  368. if (!report || report->maxfield != 1)
  369. return -ENODEV;
  370. spin_lock_irqsave(&data->lock, flags);
  371. for (i = 0; i < 4; i++) {
  372. for (j = 0; j < report->field[0]->maxusage; j++)
  373. if (j == 0)
  374. hid_set_field(report->field[0], j, i << 2);
  375. else if (j < sizeof(mapcmd))
  376. hid_set_field(report->field[0], j, mapcmd[j]);
  377. else
  378. hid_set_field(report->field[0], j, 0);
  379. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  380. }
  381. data->status |= PICOLCD_READY_FB;
  382. spin_unlock_irqrestore(&data->lock, flags);
  383. if (data->fb_bitmap) {
  384. if (clear) {
  385. memset(data->fb_vbitmap, 0, PICOLCDFB_SIZE);
  386. memset(data->fb_bitmap, 0, PICOLCDFB_SIZE*data->fb_bpp);
  387. }
  388. data->fb_force = 1;
  389. }
  390. /* schedule first output of framebuffer */
  391. if (data->fb_info)
  392. schedule_delayed_work(&data->fb_info->deferred_work, 0);
  393. return 0;
  394. }
  395. /* Update fb_vbitmap from the screen_base and send changed tiles to device */
  396. static void picolcd_fb_update(struct picolcd_data *data)
  397. {
  398. int chip, tile, n;
  399. unsigned long flags;
  400. if (!data)
  401. return;
  402. spin_lock_irqsave(&data->lock, flags);
  403. if (!(data->status & PICOLCD_READY_FB)) {
  404. spin_unlock_irqrestore(&data->lock, flags);
  405. picolcd_fb_reset(data, 0);
  406. } else {
  407. spin_unlock_irqrestore(&data->lock, flags);
  408. }
  409. /*
  410. * Translate the framebuffer into the format needed by the PicoLCD.
  411. * See display layout above.
  412. * Do this one tile after the other and push those tiles that changed.
  413. *
  414. * Wait for our IO to complete as otherwise we might flood the queue!
  415. */
  416. n = 0;
  417. for (chip = 0; chip < 4; chip++)
  418. for (tile = 0; tile < 8; tile++)
  419. if (picolcd_fb_update_tile(data->fb_vbitmap,
  420. data->fb_bitmap, data->fb_bpp, chip, tile) ||
  421. data->fb_force) {
  422. n += 2;
  423. if (!data->fb_info->par)
  424. return; /* device lost! */
  425. if (n >= HID_OUTPUT_FIFO_SIZE / 2) {
  426. usbhid_wait_io(data->hdev);
  427. n = 0;
  428. }
  429. picolcd_fb_send_tile(data->hdev, chip, tile);
  430. }
  431. data->fb_force = false;
  432. if (n)
  433. usbhid_wait_io(data->hdev);
  434. }
  435. /* Stub to call the system default and update the image on the picoLCD */
  436. static void picolcd_fb_fillrect(struct fb_info *info,
  437. const struct fb_fillrect *rect)
  438. {
  439. if (!info->par)
  440. return;
  441. sys_fillrect(info, rect);
  442. schedule_delayed_work(&info->deferred_work, 0);
  443. }
  444. /* Stub to call the system default and update the image on the picoLCD */
  445. static void picolcd_fb_copyarea(struct fb_info *info,
  446. const struct fb_copyarea *area)
  447. {
  448. if (!info->par)
  449. return;
  450. sys_copyarea(info, area);
  451. schedule_delayed_work(&info->deferred_work, 0);
  452. }
  453. /* Stub to call the system default and update the image on the picoLCD */
  454. static void picolcd_fb_imageblit(struct fb_info *info, const struct fb_image *image)
  455. {
  456. if (!info->par)
  457. return;
  458. sys_imageblit(info, image);
  459. schedule_delayed_work(&info->deferred_work, 0);
  460. }
  461. /*
  462. * this is the slow path from userspace. they can seek and write to
  463. * the fb. it's inefficient to do anything less than a full screen draw
  464. */
  465. static ssize_t picolcd_fb_write(struct fb_info *info, const char __user *buf,
  466. size_t count, loff_t *ppos)
  467. {
  468. ssize_t ret;
  469. if (!info->par)
  470. return -ENODEV;
  471. ret = fb_sys_write(info, buf, count, ppos);
  472. if (ret >= 0)
  473. schedule_delayed_work(&info->deferred_work, 0);
  474. return ret;
  475. }
  476. static int picolcd_fb_blank(int blank, struct fb_info *info)
  477. {
  478. if (!info->par)
  479. return -ENODEV;
  480. /* We let fb notification do this for us via lcd/backlight device */
  481. return 0;
  482. }
  483. static void picolcd_fb_destroy(struct fb_info *info)
  484. {
  485. struct picolcd_data *data = info->par;
  486. u32 *ref_cnt = info->pseudo_palette;
  487. int may_release;
  488. info->par = NULL;
  489. if (data)
  490. data->fb_info = NULL;
  491. fb_deferred_io_cleanup(info);
  492. ref_cnt--;
  493. mutex_lock(&info->lock);
  494. (*ref_cnt)--;
  495. may_release = !*ref_cnt;
  496. mutex_unlock(&info->lock);
  497. if (may_release) {
  498. vfree((u8 *)info->fix.smem_start);
  499. framebuffer_release(info);
  500. }
  501. }
  502. static int picolcd_fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  503. {
  504. __u32 bpp = var->bits_per_pixel;
  505. __u32 activate = var->activate;
  506. /* only allow 1/8 bit depth (8-bit is grayscale) */
  507. *var = picolcdfb_var;
  508. var->activate = activate;
  509. if (bpp >= 8) {
  510. var->bits_per_pixel = 8;
  511. var->red.length = 8;
  512. var->green.length = 8;
  513. var->blue.length = 8;
  514. } else {
  515. var->bits_per_pixel = 1;
  516. var->red.length = 1;
  517. var->green.length = 1;
  518. var->blue.length = 1;
  519. }
  520. return 0;
  521. }
  522. static int picolcd_set_par(struct fb_info *info)
  523. {
  524. struct picolcd_data *data = info->par;
  525. u8 *tmp_fb, *o_fb;
  526. if (!data)
  527. return -ENODEV;
  528. if (info->var.bits_per_pixel == data->fb_bpp)
  529. return 0;
  530. /* switch between 1/8 bit depths */
  531. if (info->var.bits_per_pixel != 1 && info->var.bits_per_pixel != 8)
  532. return -EINVAL;
  533. o_fb = data->fb_bitmap;
  534. tmp_fb = kmalloc(PICOLCDFB_SIZE*info->var.bits_per_pixel, GFP_KERNEL);
  535. if (!tmp_fb)
  536. return -ENOMEM;
  537. /* translate FB content to new bits-per-pixel */
  538. if (info->var.bits_per_pixel == 1) {
  539. int i, b;
  540. for (i = 0; i < PICOLCDFB_SIZE; i++) {
  541. u8 p = 0;
  542. for (b = 0; b < 8; b++) {
  543. p <<= 1;
  544. p |= o_fb[i*8+b] ? 0x01 : 0x00;
  545. }
  546. tmp_fb[i] = p;
  547. }
  548. memcpy(o_fb, tmp_fb, PICOLCDFB_SIZE);
  549. info->fix.visual = FB_VISUAL_MONO01;
  550. info->fix.line_length = PICOLCDFB_WIDTH / 8;
  551. } else {
  552. int i;
  553. memcpy(tmp_fb, o_fb, PICOLCDFB_SIZE);
  554. for (i = 0; i < PICOLCDFB_SIZE * 8; i++)
  555. o_fb[i] = tmp_fb[i/8] & (0x01 << (7 - i % 8)) ? 0xff : 0x00;
  556. info->fix.visual = FB_VISUAL_DIRECTCOLOR;
  557. info->fix.line_length = PICOLCDFB_WIDTH;
  558. }
  559. kfree(tmp_fb);
  560. data->fb_bpp = info->var.bits_per_pixel;
  561. return 0;
  562. }
  563. /* Do refcounting on our FB and cleanup per worker if FB is
  564. * closed after unplug of our device
  565. * (fb_release holds info->lock and still touches info after
  566. * we return so we can't release it immediately.
  567. */
  568. struct picolcd_fb_cleanup_item {
  569. struct fb_info *info;
  570. struct picolcd_fb_cleanup_item *next;
  571. };
  572. static struct picolcd_fb_cleanup_item *fb_pending;
  573. static DEFINE_SPINLOCK(fb_pending_lock);
  574. static void picolcd_fb_do_cleanup(struct work_struct *data)
  575. {
  576. struct picolcd_fb_cleanup_item *item;
  577. unsigned long flags;
  578. do {
  579. spin_lock_irqsave(&fb_pending_lock, flags);
  580. item = fb_pending;
  581. fb_pending = item ? item->next : NULL;
  582. spin_unlock_irqrestore(&fb_pending_lock, flags);
  583. if (item) {
  584. u8 *fb = (u8 *)item->info->fix.smem_start;
  585. /* make sure we do not race against fb core when
  586. * releasing */
  587. mutex_lock(&item->info->lock);
  588. mutex_unlock(&item->info->lock);
  589. framebuffer_release(item->info);
  590. vfree(fb);
  591. }
  592. } while (item);
  593. }
  594. static DECLARE_WORK(picolcd_fb_cleanup, picolcd_fb_do_cleanup);
  595. static int picolcd_fb_open(struct fb_info *info, int u)
  596. {
  597. u32 *ref_cnt = info->pseudo_palette;
  598. ref_cnt--;
  599. (*ref_cnt)++;
  600. return 0;
  601. }
  602. static int picolcd_fb_release(struct fb_info *info, int u)
  603. {
  604. u32 *ref_cnt = info->pseudo_palette;
  605. ref_cnt--;
  606. (*ref_cnt)++;
  607. if (!*ref_cnt) {
  608. unsigned long flags;
  609. struct picolcd_fb_cleanup_item *item = (struct picolcd_fb_cleanup_item *)ref_cnt;
  610. item--;
  611. spin_lock_irqsave(&fb_pending_lock, flags);
  612. item->next = fb_pending;
  613. fb_pending = item;
  614. spin_unlock_irqrestore(&fb_pending_lock, flags);
  615. schedule_work(&picolcd_fb_cleanup);
  616. }
  617. return 0;
  618. }
  619. /* Note this can't be const because of struct fb_info definition */
  620. static struct fb_ops picolcdfb_ops = {
  621. .owner = THIS_MODULE,
  622. .fb_destroy = picolcd_fb_destroy,
  623. .fb_open = picolcd_fb_open,
  624. .fb_release = picolcd_fb_release,
  625. .fb_read = fb_sys_read,
  626. .fb_write = picolcd_fb_write,
  627. .fb_blank = picolcd_fb_blank,
  628. .fb_fillrect = picolcd_fb_fillrect,
  629. .fb_copyarea = picolcd_fb_copyarea,
  630. .fb_imageblit = picolcd_fb_imageblit,
  631. .fb_check_var = picolcd_fb_check_var,
  632. .fb_set_par = picolcd_set_par,
  633. };
  634. /* Callback from deferred IO workqueue */
  635. static void picolcd_fb_deferred_io(struct fb_info *info, struct list_head *pagelist)
  636. {
  637. picolcd_fb_update(info->par);
  638. }
  639. static const struct fb_deferred_io picolcd_fb_defio = {
  640. .delay = HZ / PICOLCDFB_UPDATE_RATE_DEFAULT,
  641. .deferred_io = picolcd_fb_deferred_io,
  642. };
  643. /*
  644. * The "fb_update_rate" sysfs attribute
  645. */
  646. static ssize_t picolcd_fb_update_rate_show(struct device *dev,
  647. struct device_attribute *attr, char *buf)
  648. {
  649. struct picolcd_data *data = dev_get_drvdata(dev);
  650. unsigned i, fb_update_rate = data->fb_update_rate;
  651. size_t ret = 0;
  652. for (i = 1; i <= PICOLCDFB_UPDATE_RATE_LIMIT; i++)
  653. if (ret >= PAGE_SIZE)
  654. break;
  655. else if (i == fb_update_rate)
  656. ret += snprintf(buf+ret, PAGE_SIZE-ret, "[%u] ", i);
  657. else
  658. ret += snprintf(buf+ret, PAGE_SIZE-ret, "%u ", i);
  659. if (ret > 0)
  660. buf[min(ret, (size_t)PAGE_SIZE)-1] = '\n';
  661. return ret;
  662. }
  663. static ssize_t picolcd_fb_update_rate_store(struct device *dev,
  664. struct device_attribute *attr, const char *buf, size_t count)
  665. {
  666. struct picolcd_data *data = dev_get_drvdata(dev);
  667. int i;
  668. unsigned u;
  669. if (count < 1 || count > 10)
  670. return -EINVAL;
  671. i = sscanf(buf, "%u", &u);
  672. if (i != 1)
  673. return -EINVAL;
  674. if (u > PICOLCDFB_UPDATE_RATE_LIMIT)
  675. return -ERANGE;
  676. else if (u == 0)
  677. u = PICOLCDFB_UPDATE_RATE_DEFAULT;
  678. data->fb_update_rate = u;
  679. data->fb_defio.delay = HZ / data->fb_update_rate;
  680. return count;
  681. }
  682. static DEVICE_ATTR(fb_update_rate, 0666, picolcd_fb_update_rate_show,
  683. picolcd_fb_update_rate_store);
  684. /* initialize Framebuffer device */
  685. static int picolcd_init_framebuffer(struct picolcd_data *data)
  686. {
  687. struct device *dev = &data->hdev->dev;
  688. struct fb_info *info = NULL;
  689. int i, error = -ENOMEM;
  690. u8 *fb_vbitmap = NULL;
  691. u8 *fb_bitmap = NULL;
  692. u32 *palette;
  693. fb_bitmap = vmalloc(PICOLCDFB_SIZE*8);
  694. if (fb_bitmap == NULL) {
  695. dev_err(dev, "can't get a free page for framebuffer\n");
  696. goto err_nomem;
  697. }
  698. fb_vbitmap = kmalloc(PICOLCDFB_SIZE, GFP_KERNEL);
  699. if (fb_vbitmap == NULL) {
  700. dev_err(dev, "can't alloc vbitmap image buffer\n");
  701. goto err_nomem;
  702. }
  703. data->fb_update_rate = PICOLCDFB_UPDATE_RATE_DEFAULT;
  704. data->fb_defio = picolcd_fb_defio;
  705. /* The extra memory is:
  706. * - struct picolcd_fb_cleanup_item
  707. * - u32 for ref_count
  708. * - 256*u32 for pseudo_palette
  709. */
  710. info = framebuffer_alloc(257 * sizeof(u32) + sizeof(struct picolcd_fb_cleanup_item), dev);
  711. if (info == NULL) {
  712. dev_err(dev, "failed to allocate a framebuffer\n");
  713. goto err_nomem;
  714. }
  715. palette = info->par + sizeof(struct picolcd_fb_cleanup_item);
  716. *palette = 1;
  717. palette++;
  718. for (i = 0; i < 256; i++)
  719. palette[i] = i > 0 && i < 16 ? 0xff : 0;
  720. info->pseudo_palette = palette;
  721. info->fbdefio = &data->fb_defio;
  722. info->screen_base = (char __force __iomem *)fb_bitmap;
  723. info->fbops = &picolcdfb_ops;
  724. info->var = picolcdfb_var;
  725. info->fix = picolcdfb_fix;
  726. info->fix.smem_len = PICOLCDFB_SIZE*8;
  727. info->fix.smem_start = (unsigned long)fb_bitmap;
  728. info->par = data;
  729. info->flags = FBINFO_FLAG_DEFAULT;
  730. data->fb_vbitmap = fb_vbitmap;
  731. data->fb_bitmap = fb_bitmap;
  732. data->fb_bpp = picolcdfb_var.bits_per_pixel;
  733. error = picolcd_fb_reset(data, 1);
  734. if (error) {
  735. dev_err(dev, "failed to configure display\n");
  736. goto err_cleanup;
  737. }
  738. error = device_create_file(dev, &dev_attr_fb_update_rate);
  739. if (error) {
  740. dev_err(dev, "failed to create sysfs attributes\n");
  741. goto err_cleanup;
  742. }
  743. fb_deferred_io_init(info);
  744. data->fb_info = info;
  745. error = register_framebuffer(info);
  746. if (error) {
  747. dev_err(dev, "failed to register framebuffer\n");
  748. goto err_sysfs;
  749. }
  750. /* schedule first output of framebuffer */
  751. data->fb_force = 1;
  752. schedule_delayed_work(&info->deferred_work, 0);
  753. return 0;
  754. err_sysfs:
  755. fb_deferred_io_cleanup(info);
  756. device_remove_file(dev, &dev_attr_fb_update_rate);
  757. err_cleanup:
  758. data->fb_vbitmap = NULL;
  759. data->fb_bitmap = NULL;
  760. data->fb_bpp = 0;
  761. data->fb_info = NULL;
  762. err_nomem:
  763. framebuffer_release(info);
  764. vfree(fb_bitmap);
  765. kfree(fb_vbitmap);
  766. return error;
  767. }
  768. static void picolcd_exit_framebuffer(struct picolcd_data *data)
  769. {
  770. struct fb_info *info = data->fb_info;
  771. u8 *fb_vbitmap = data->fb_vbitmap;
  772. if (!info)
  773. return;
  774. info->par = NULL;
  775. device_remove_file(&data->hdev->dev, &dev_attr_fb_update_rate);
  776. unregister_framebuffer(info);
  777. data->fb_vbitmap = NULL;
  778. data->fb_bitmap = NULL;
  779. data->fb_bpp = 0;
  780. data->fb_info = NULL;
  781. kfree(fb_vbitmap);
  782. }
  783. #define picolcd_fbinfo(d) ((d)->fb_info)
  784. #else
  785. static inline int picolcd_fb_reset(struct picolcd_data *data, int clear)
  786. {
  787. return 0;
  788. }
  789. static inline int picolcd_init_framebuffer(struct picolcd_data *data)
  790. {
  791. return 0;
  792. }
  793. static inline void picolcd_exit_framebuffer(struct picolcd_data *data)
  794. {
  795. }
  796. #define picolcd_fbinfo(d) NULL
  797. #endif /* CONFIG_HID_PICOLCD_FB */
  798. #ifdef CONFIG_HID_PICOLCD_BACKLIGHT
  799. /*
  800. * backlight class device
  801. */
  802. static int picolcd_get_brightness(struct backlight_device *bdev)
  803. {
  804. struct picolcd_data *data = bl_get_data(bdev);
  805. return data->lcd_brightness;
  806. }
  807. static int picolcd_set_brightness(struct backlight_device *bdev)
  808. {
  809. struct picolcd_data *data = bl_get_data(bdev);
  810. struct hid_report *report = picolcd_out_report(REPORT_BRIGHTNESS, data->hdev);
  811. unsigned long flags;
  812. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  813. return -ENODEV;
  814. data->lcd_brightness = bdev->props.brightness & 0x0ff;
  815. data->lcd_power = bdev->props.power;
  816. spin_lock_irqsave(&data->lock, flags);
  817. hid_set_field(report->field[0], 0, data->lcd_power == FB_BLANK_UNBLANK ? data->lcd_brightness : 0);
  818. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  819. spin_unlock_irqrestore(&data->lock, flags);
  820. return 0;
  821. }
  822. static int picolcd_check_bl_fb(struct backlight_device *bdev, struct fb_info *fb)
  823. {
  824. return fb && fb == picolcd_fbinfo((struct picolcd_data *)bl_get_data(bdev));
  825. }
  826. static const struct backlight_ops picolcd_blops = {
  827. .update_status = picolcd_set_brightness,
  828. .get_brightness = picolcd_get_brightness,
  829. .check_fb = picolcd_check_bl_fb,
  830. };
  831. static int picolcd_init_backlight(struct picolcd_data *data, struct hid_report *report)
  832. {
  833. struct device *dev = &data->hdev->dev;
  834. struct backlight_device *bdev;
  835. struct backlight_properties props;
  836. if (!report)
  837. return -ENODEV;
  838. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  839. report->field[0]->report_size != 8) {
  840. dev_err(dev, "unsupported BRIGHTNESS report");
  841. return -EINVAL;
  842. }
  843. memset(&props, 0, sizeof(props));
  844. props.type = BACKLIGHT_RAW;
  845. props.max_brightness = 0xff;
  846. bdev = backlight_device_register(dev_name(dev), dev, data,
  847. &picolcd_blops, &props);
  848. if (IS_ERR(bdev)) {
  849. dev_err(dev, "failed to register backlight\n");
  850. return PTR_ERR(bdev);
  851. }
  852. bdev->props.brightness = 0xff;
  853. data->lcd_brightness = 0xff;
  854. data->backlight = bdev;
  855. picolcd_set_brightness(bdev);
  856. return 0;
  857. }
  858. static void picolcd_exit_backlight(struct picolcd_data *data)
  859. {
  860. struct backlight_device *bdev = data->backlight;
  861. data->backlight = NULL;
  862. if (bdev)
  863. backlight_device_unregister(bdev);
  864. }
  865. static inline int picolcd_resume_backlight(struct picolcd_data *data)
  866. {
  867. if (!data->backlight)
  868. return 0;
  869. return picolcd_set_brightness(data->backlight);
  870. }
  871. #ifdef CONFIG_PM
  872. static void picolcd_suspend_backlight(struct picolcd_data *data)
  873. {
  874. int bl_power = data->lcd_power;
  875. if (!data->backlight)
  876. return;
  877. data->backlight->props.power = FB_BLANK_POWERDOWN;
  878. picolcd_set_brightness(data->backlight);
  879. data->lcd_power = data->backlight->props.power = bl_power;
  880. }
  881. #endif /* CONFIG_PM */
  882. #else
  883. static inline int picolcd_init_backlight(struct picolcd_data *data,
  884. struct hid_report *report)
  885. {
  886. return 0;
  887. }
  888. static inline void picolcd_exit_backlight(struct picolcd_data *data)
  889. {
  890. }
  891. static inline int picolcd_resume_backlight(struct picolcd_data *data)
  892. {
  893. return 0;
  894. }
  895. static inline void picolcd_suspend_backlight(struct picolcd_data *data)
  896. {
  897. }
  898. #endif /* CONFIG_HID_PICOLCD_BACKLIGHT */
  899. #ifdef CONFIG_HID_PICOLCD_LCD
  900. /*
  901. * lcd class device
  902. */
  903. static int picolcd_get_contrast(struct lcd_device *ldev)
  904. {
  905. struct picolcd_data *data = lcd_get_data(ldev);
  906. return data->lcd_contrast;
  907. }
  908. static int picolcd_set_contrast(struct lcd_device *ldev, int contrast)
  909. {
  910. struct picolcd_data *data = lcd_get_data(ldev);
  911. struct hid_report *report = picolcd_out_report(REPORT_CONTRAST, data->hdev);
  912. unsigned long flags;
  913. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  914. return -ENODEV;
  915. data->lcd_contrast = contrast & 0x0ff;
  916. spin_lock_irqsave(&data->lock, flags);
  917. hid_set_field(report->field[0], 0, data->lcd_contrast);
  918. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  919. spin_unlock_irqrestore(&data->lock, flags);
  920. return 0;
  921. }
  922. static int picolcd_check_lcd_fb(struct lcd_device *ldev, struct fb_info *fb)
  923. {
  924. return fb && fb == picolcd_fbinfo((struct picolcd_data *)lcd_get_data(ldev));
  925. }
  926. static struct lcd_ops picolcd_lcdops = {
  927. .get_contrast = picolcd_get_contrast,
  928. .set_contrast = picolcd_set_contrast,
  929. .check_fb = picolcd_check_lcd_fb,
  930. };
  931. static int picolcd_init_lcd(struct picolcd_data *data, struct hid_report *report)
  932. {
  933. struct device *dev = &data->hdev->dev;
  934. struct lcd_device *ldev;
  935. if (!report)
  936. return -ENODEV;
  937. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  938. report->field[0]->report_size != 8) {
  939. dev_err(dev, "unsupported CONTRAST report");
  940. return -EINVAL;
  941. }
  942. ldev = lcd_device_register(dev_name(dev), dev, data, &picolcd_lcdops);
  943. if (IS_ERR(ldev)) {
  944. dev_err(dev, "failed to register LCD\n");
  945. return PTR_ERR(ldev);
  946. }
  947. ldev->props.max_contrast = 0x0ff;
  948. data->lcd_contrast = 0xe5;
  949. data->lcd = ldev;
  950. picolcd_set_contrast(ldev, 0xe5);
  951. return 0;
  952. }
  953. static void picolcd_exit_lcd(struct picolcd_data *data)
  954. {
  955. struct lcd_device *ldev = data->lcd;
  956. data->lcd = NULL;
  957. if (ldev)
  958. lcd_device_unregister(ldev);
  959. }
  960. static inline int picolcd_resume_lcd(struct picolcd_data *data)
  961. {
  962. if (!data->lcd)
  963. return 0;
  964. return picolcd_set_contrast(data->lcd, data->lcd_contrast);
  965. }
  966. #else
  967. static inline int picolcd_init_lcd(struct picolcd_data *data,
  968. struct hid_report *report)
  969. {
  970. return 0;
  971. }
  972. static inline void picolcd_exit_lcd(struct picolcd_data *data)
  973. {
  974. }
  975. static inline int picolcd_resume_lcd(struct picolcd_data *data)
  976. {
  977. return 0;
  978. }
  979. #endif /* CONFIG_HID_PICOLCD_LCD */
  980. #ifdef CONFIG_HID_PICOLCD_LEDS
  981. /**
  982. * LED class device
  983. */
  984. static void picolcd_leds_set(struct picolcd_data *data)
  985. {
  986. struct hid_report *report;
  987. unsigned long flags;
  988. if (!data->led[0])
  989. return;
  990. report = picolcd_out_report(REPORT_LED_STATE, data->hdev);
  991. if (!report || report->maxfield != 1 || report->field[0]->report_count != 1)
  992. return;
  993. spin_lock_irqsave(&data->lock, flags);
  994. hid_set_field(report->field[0], 0, data->led_state);
  995. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  996. spin_unlock_irqrestore(&data->lock, flags);
  997. }
  998. static void picolcd_led_set_brightness(struct led_classdev *led_cdev,
  999. enum led_brightness value)
  1000. {
  1001. struct device *dev;
  1002. struct hid_device *hdev;
  1003. struct picolcd_data *data;
  1004. int i, state = 0;
  1005. dev = led_cdev->dev->parent;
  1006. hdev = container_of(dev, struct hid_device, dev);
  1007. data = hid_get_drvdata(hdev);
  1008. for (i = 0; i < 8; i++) {
  1009. if (led_cdev != data->led[i])
  1010. continue;
  1011. state = (data->led_state >> i) & 1;
  1012. if (value == LED_OFF && state) {
  1013. data->led_state &= ~(1 << i);
  1014. picolcd_leds_set(data);
  1015. } else if (value != LED_OFF && !state) {
  1016. data->led_state |= 1 << i;
  1017. picolcd_leds_set(data);
  1018. }
  1019. break;
  1020. }
  1021. }
  1022. static enum led_brightness picolcd_led_get_brightness(struct led_classdev *led_cdev)
  1023. {
  1024. struct device *dev;
  1025. struct hid_device *hdev;
  1026. struct picolcd_data *data;
  1027. int i, value = 0;
  1028. dev = led_cdev->dev->parent;
  1029. hdev = container_of(dev, struct hid_device, dev);
  1030. data = hid_get_drvdata(hdev);
  1031. for (i = 0; i < 8; i++)
  1032. if (led_cdev == data->led[i]) {
  1033. value = (data->led_state >> i) & 1;
  1034. break;
  1035. }
  1036. return value ? LED_FULL : LED_OFF;
  1037. }
  1038. static int picolcd_init_leds(struct picolcd_data *data, struct hid_report *report)
  1039. {
  1040. struct device *dev = &data->hdev->dev;
  1041. struct led_classdev *led;
  1042. size_t name_sz = strlen(dev_name(dev)) + 8;
  1043. char *name;
  1044. int i, ret = 0;
  1045. if (!report)
  1046. return -ENODEV;
  1047. if (report->maxfield != 1 || report->field[0]->report_count != 1 ||
  1048. report->field[0]->report_size != 8) {
  1049. dev_err(dev, "unsupported LED_STATE report");
  1050. return -EINVAL;
  1051. }
  1052. for (i = 0; i < 8; i++) {
  1053. led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
  1054. if (!led) {
  1055. dev_err(dev, "can't allocate memory for LED %d\n", i);
  1056. ret = -ENOMEM;
  1057. goto err;
  1058. }
  1059. name = (void *)(&led[1]);
  1060. snprintf(name, name_sz, "%s::GPO%d", dev_name(dev), i);
  1061. led->name = name;
  1062. led->brightness = 0;
  1063. led->max_brightness = 1;
  1064. led->brightness_get = picolcd_led_get_brightness;
  1065. led->brightness_set = picolcd_led_set_brightness;
  1066. data->led[i] = led;
  1067. ret = led_classdev_register(dev, data->led[i]);
  1068. if (ret) {
  1069. data->led[i] = NULL;
  1070. kfree(led);
  1071. dev_err(dev, "can't register LED %d\n", i);
  1072. goto err;
  1073. }
  1074. }
  1075. return 0;
  1076. err:
  1077. for (i = 0; i < 8; i++)
  1078. if (data->led[i]) {
  1079. led = data->led[i];
  1080. data->led[i] = NULL;
  1081. led_classdev_unregister(led);
  1082. kfree(led);
  1083. }
  1084. return ret;
  1085. }
  1086. static void picolcd_exit_leds(struct picolcd_data *data)
  1087. {
  1088. struct led_classdev *led;
  1089. int i;
  1090. for (i = 0; i < 8; i++) {
  1091. led = data->led[i];
  1092. data->led[i] = NULL;
  1093. if (!led)
  1094. continue;
  1095. led_classdev_unregister(led);
  1096. kfree(led);
  1097. }
  1098. }
  1099. #else
  1100. static inline int picolcd_init_leds(struct picolcd_data *data,
  1101. struct hid_report *report)
  1102. {
  1103. return 0;
  1104. }
  1105. static inline void picolcd_exit_leds(struct picolcd_data *data)
  1106. {
  1107. }
  1108. static inline int picolcd_leds_set(struct picolcd_data *data)
  1109. {
  1110. return 0;
  1111. }
  1112. #endif /* CONFIG_HID_PICOLCD_LEDS */
  1113. /*
  1114. * input class device
  1115. */
  1116. static int picolcd_raw_keypad(struct picolcd_data *data,
  1117. struct hid_report *report, u8 *raw_data, int size)
  1118. {
  1119. /*
  1120. * Keypad event
  1121. * First and second data bytes list currently pressed keys,
  1122. * 0x00 means no key and at most 2 keys may be pressed at same time
  1123. */
  1124. int i, j;
  1125. /* determine newly pressed keys */
  1126. for (i = 0; i < size; i++) {
  1127. unsigned int key_code;
  1128. if (raw_data[i] == 0)
  1129. continue;
  1130. for (j = 0; j < sizeof(data->pressed_keys); j++)
  1131. if (data->pressed_keys[j] == raw_data[i])
  1132. goto key_already_down;
  1133. for (j = 0; j < sizeof(data->pressed_keys); j++)
  1134. if (data->pressed_keys[j] == 0) {
  1135. data->pressed_keys[j] = raw_data[i];
  1136. break;
  1137. }
  1138. input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]);
  1139. if (raw_data[i] < PICOLCD_KEYS)
  1140. key_code = data->keycode[raw_data[i]];
  1141. else
  1142. key_code = KEY_UNKNOWN;
  1143. if (key_code != KEY_UNKNOWN) {
  1144. dbg_hid(PICOLCD_NAME " got key press for %u:%d",
  1145. raw_data[i], key_code);
  1146. input_report_key(data->input_keys, key_code, 1);
  1147. }
  1148. input_sync(data->input_keys);
  1149. key_already_down:
  1150. continue;
  1151. }
  1152. /* determine newly released keys */
  1153. for (j = 0; j < sizeof(data->pressed_keys); j++) {
  1154. unsigned int key_code;
  1155. if (data->pressed_keys[j] == 0)
  1156. continue;
  1157. for (i = 0; i < size; i++)
  1158. if (data->pressed_keys[j] == raw_data[i])
  1159. goto key_still_down;
  1160. input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]);
  1161. if (data->pressed_keys[j] < PICOLCD_KEYS)
  1162. key_code = data->keycode[data->pressed_keys[j]];
  1163. else
  1164. key_code = KEY_UNKNOWN;
  1165. if (key_code != KEY_UNKNOWN) {
  1166. dbg_hid(PICOLCD_NAME " got key release for %u:%d",
  1167. data->pressed_keys[j], key_code);
  1168. input_report_key(data->input_keys, key_code, 0);
  1169. }
  1170. input_sync(data->input_keys);
  1171. data->pressed_keys[j] = 0;
  1172. key_still_down:
  1173. continue;
  1174. }
  1175. return 1;
  1176. }
  1177. static int picolcd_raw_cir(struct picolcd_data *data,
  1178. struct hid_report *report, u8 *raw_data, int size)
  1179. {
  1180. /* Need understanding of CIR data format to implement ... */
  1181. return 1;
  1182. }
  1183. static int picolcd_check_version(struct hid_device *hdev)
  1184. {
  1185. struct picolcd_data *data = hid_get_drvdata(hdev);
  1186. struct picolcd_pending *verinfo;
  1187. int ret = 0;
  1188. if (!data)
  1189. return -ENODEV;
  1190. verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0);
  1191. if (!verinfo) {
  1192. hid_err(hdev, "no version response from PicoLCD\n");
  1193. return -ENODEV;
  1194. }
  1195. if (verinfo->raw_size == 2) {
  1196. data->version[0] = verinfo->raw_data[1];
  1197. data->version[1] = verinfo->raw_data[0];
  1198. if (data->status & PICOLCD_BOOTLOADER) {
  1199. hid_info(hdev, "PicoLCD, bootloader version %d.%d\n",
  1200. verinfo->raw_data[1], verinfo->raw_data[0]);
  1201. } else {
  1202. hid_info(hdev, "PicoLCD, firmware version %d.%d\n",
  1203. verinfo->raw_data[1], verinfo->raw_data[0]);
  1204. }
  1205. } else {
  1206. hid_err(hdev, "confused, got unexpected version response from PicoLCD\n");
  1207. ret = -EINVAL;
  1208. }
  1209. kfree(verinfo);
  1210. return ret;
  1211. }
  1212. /*
  1213. * Reset our device and wait for answer to VERSION request
  1214. */
  1215. static int picolcd_reset(struct hid_device *hdev)
  1216. {
  1217. struct picolcd_data *data = hid_get_drvdata(hdev);
  1218. struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev);
  1219. unsigned long flags;
  1220. int error;
  1221. if (!data || !report || report->maxfield != 1)
  1222. return -ENODEV;
  1223. spin_lock_irqsave(&data->lock, flags);
  1224. if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
  1225. data->status |= PICOLCD_BOOTLOADER;
  1226. /* perform the reset */
  1227. hid_set_field(report->field[0], 0, 1);
  1228. usbhid_submit_report(hdev, report, USB_DIR_OUT);
  1229. spin_unlock_irqrestore(&data->lock, flags);
  1230. error = picolcd_check_version(hdev);
  1231. if (error)
  1232. return error;
  1233. picolcd_resume_lcd(data);
  1234. picolcd_resume_backlight(data);
  1235. #ifdef CONFIG_HID_PICOLCD_FB
  1236. if (data->fb_info)
  1237. schedule_delayed_work(&data->fb_info->deferred_work, 0);
  1238. #endif /* CONFIG_HID_PICOLCD_FB */
  1239. picolcd_leds_set(data);
  1240. return 0;
  1241. }
  1242. /*
  1243. * The "operation_mode" sysfs attribute
  1244. */
  1245. static ssize_t picolcd_operation_mode_show(struct device *dev,
  1246. struct device_attribute *attr, char *buf)
  1247. {
  1248. struct picolcd_data *data = dev_get_drvdata(dev);
  1249. if (data->status & PICOLCD_BOOTLOADER)
  1250. return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n");
  1251. else
  1252. return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n");
  1253. }
  1254. static ssize_t picolcd_operation_mode_store(struct device *dev,
  1255. struct device_attribute *attr, const char *buf, size_t count)
  1256. {
  1257. struct picolcd_data *data = dev_get_drvdata(dev);
  1258. struct hid_report *report = NULL;
  1259. size_t cnt = count;
  1260. int timeout = data->opmode_delay;
  1261. unsigned long flags;
  1262. if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) {
  1263. if (data->status & PICOLCD_BOOTLOADER)
  1264. report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev);
  1265. buf += 3;
  1266. cnt -= 3;
  1267. } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) {
  1268. if (!(data->status & PICOLCD_BOOTLOADER))
  1269. report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev);
  1270. buf += 10;
  1271. cnt -= 10;
  1272. }
  1273. if (!report || report->maxfield != 1)
  1274. return -EINVAL;
  1275. while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r'))
  1276. cnt--;
  1277. if (cnt != 0)
  1278. return -EINVAL;
  1279. spin_lock_irqsave(&data->lock, flags);
  1280. hid_set_field(report->field[0], 0, timeout & 0xff);
  1281. hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff);
  1282. usbhid_submit_report(data->hdev, report, USB_DIR_OUT);
  1283. spin_unlock_irqrestore(&data->lock, flags);
  1284. return count;
  1285. }
  1286. static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show,
  1287. picolcd_operation_mode_store);
  1288. /*
  1289. * The "operation_mode_delay" sysfs attribute
  1290. */
  1291. static ssize_t picolcd_operation_mode_delay_show(struct device *dev,
  1292. struct device_attribute *attr, char *buf)
  1293. {
  1294. struct picolcd_data *data = dev_get_drvdata(dev);
  1295. return snprintf(buf, PAGE_SIZE, "%hu\n", data->opmode_delay);
  1296. }
  1297. static ssize_t picolcd_operation_mode_delay_store(struct device *dev,
  1298. struct device_attribute *attr, const char *buf, size_t count)
  1299. {
  1300. struct picolcd_data *data = dev_get_drvdata(dev);
  1301. unsigned u;
  1302. if (sscanf(buf, "%u", &u) != 1)
  1303. return -EINVAL;
  1304. if (u > 30000)
  1305. return -EINVAL;
  1306. else
  1307. data->opmode_delay = u;
  1308. return count;
  1309. }
  1310. static DEVICE_ATTR(operation_mode_delay, 0644, picolcd_operation_mode_delay_show,
  1311. picolcd_operation_mode_delay_store);
  1312. #ifdef CONFIG_DEBUG_FS
  1313. /*
  1314. * The "reset" file
  1315. */
  1316. static int picolcd_debug_reset_show(struct seq_file *f, void *p)
  1317. {
  1318. if (picolcd_fbinfo((struct picolcd_data *)f->private))
  1319. seq_printf(f, "all fb\n");
  1320. else
  1321. seq_printf(f, "all\n");
  1322. return 0;
  1323. }
  1324. static int picolcd_debug_reset_open(struct inode *inode, struct file *f)
  1325. {
  1326. return single_open(f, picolcd_debug_reset_show, inode->i_private);
  1327. }
  1328. static ssize_t picolcd_debug_reset_write(struct file *f, const char __user *user_buf,
  1329. size_t count, loff_t *ppos)
  1330. {
  1331. struct picolcd_data *data = ((struct seq_file *)f->private_data)->private;
  1332. char buf[32];
  1333. size_t cnt = min(count, sizeof(buf)-1);
  1334. if (copy_from_user(buf, user_buf, cnt))
  1335. return -EFAULT;
  1336. while (cnt > 0 && (buf[cnt-1] == ' ' || buf[cnt-1] == '\n'))
  1337. cnt--;
  1338. buf[cnt] = '\0';
  1339. if (strcmp(buf, "all") == 0) {
  1340. picolcd_reset(data->hdev);
  1341. picolcd_fb_reset(data, 1);
  1342. } else if (strcmp(buf, "fb") == 0) {
  1343. picolcd_fb_reset(data, 1);
  1344. } else {
  1345. return -EINVAL;
  1346. }
  1347. return count;
  1348. }
  1349. static const struct file_operations picolcd_debug_reset_fops = {
  1350. .owner = THIS_MODULE,
  1351. .open = picolcd_debug_reset_open,
  1352. .read = seq_read,
  1353. .llseek = seq_lseek,
  1354. .write = picolcd_debug_reset_write,
  1355. .release = single_release,
  1356. };
  1357. /*
  1358. * The "eeprom" file
  1359. */
  1360. static ssize_t picolcd_debug_eeprom_read(struct file *f, char __user *u,
  1361. size_t s, loff_t *off)
  1362. {
  1363. struct picolcd_data *data = f->private_data;
  1364. struct picolcd_pending *resp;
  1365. u8 raw_data[3];
  1366. ssize_t ret = -EIO;
  1367. if (s == 0)
  1368. return -EINVAL;
  1369. if (*off > 0x0ff)
  1370. return 0;
  1371. /* prepare buffer with info about what we want to read (addr & len) */
  1372. raw_data[0] = *off & 0xff;
  1373. raw_data[1] = (*off >> 8) & 0xff;
  1374. raw_data[2] = s < 20 ? s : 20;
  1375. if (*off + raw_data[2] > 0xff)
  1376. raw_data[2] = 0x100 - *off;
  1377. resp = picolcd_send_and_wait(data->hdev, REPORT_EE_READ, raw_data,
  1378. sizeof(raw_data));
  1379. if (!resp)
  1380. return -EIO;
  1381. if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
  1382. /* successful read :) */
  1383. ret = resp->raw_data[2];
  1384. if (ret > s)
  1385. ret = s;
  1386. if (copy_to_user(u, resp->raw_data+3, ret))
  1387. ret = -EFAULT;
  1388. else
  1389. *off += ret;
  1390. } /* anything else is some kind of IO error */
  1391. kfree(resp);
  1392. return ret;
  1393. }
  1394. static ssize_t picolcd_debug_eeprom_write(struct file *f, const char __user *u,
  1395. size_t s, loff_t *off)
  1396. {
  1397. struct picolcd_data *data = f->private_data;
  1398. struct picolcd_pending *resp;
  1399. ssize_t ret = -EIO;
  1400. u8 raw_data[23];
  1401. if (s == 0)
  1402. return -EINVAL;
  1403. if (*off > 0x0ff)
  1404. return -ENOSPC;
  1405. memset(raw_data, 0, sizeof(raw_data));
  1406. raw_data[0] = *off & 0xff;
  1407. raw_data[1] = (*off >> 8) & 0xff;
  1408. raw_data[2] = min((size_t)20, s);
  1409. if (*off + raw_data[2] > 0xff)
  1410. raw_data[2] = 0x100 - *off;
  1411. if (copy_from_user(raw_data+3, u, min((u8)20, raw_data[2])))
  1412. return -EFAULT;
  1413. resp = picolcd_send_and_wait(data->hdev, REPORT_EE_WRITE, raw_data,
  1414. sizeof(raw_data));
  1415. if (!resp)
  1416. return -EIO;
  1417. if (resp->in_report && resp->in_report->id == REPORT_EE_DATA) {
  1418. /* check if written data matches */
  1419. if (memcmp(raw_data, resp->raw_data, 3+raw_data[2]) == 0) {
  1420. *off += raw_data[2];
  1421. ret = raw_data[2];
  1422. }
  1423. }
  1424. kfree(resp);
  1425. return ret;
  1426. }
  1427. /*
  1428. * Notes:
  1429. * - read/write happens in chunks of at most 20 bytes, it's up to userspace
  1430. * to loop in order to get more data.
  1431. * - on write errors on otherwise correct write request the bytes
  1432. * that should have been written are in undefined state.
  1433. */
  1434. static const struct file_operations picolcd_debug_eeprom_fops = {
  1435. .owner = THIS_MODULE,
  1436. .open = simple_open,
  1437. .read = picolcd_debug_eeprom_read,
  1438. .write = picolcd_debug_eeprom_write,
  1439. .llseek = generic_file_llseek,
  1440. };
  1441. /*
  1442. * The "flash" file
  1443. */
  1444. /* record a flash address to buf (bounds check to be done by caller) */
  1445. static int _picolcd_flash_setaddr(struct picolcd_data *data, u8 *buf, long off)
  1446. {
  1447. buf[0] = off & 0xff;
  1448. buf[1] = (off >> 8) & 0xff;
  1449. if (data->addr_sz == 3)
  1450. buf[2] = (off >> 16) & 0xff;
  1451. return data->addr_sz == 2 ? 2 : 3;
  1452. }
  1453. /* read a given size of data (bounds check to be done by caller) */
  1454. static ssize_t _picolcd_flash_read(struct picolcd_data *data, int report_id,
  1455. char __user *u, size_t s, loff_t *off)
  1456. {
  1457. struct picolcd_pending *resp;
  1458. u8 raw_data[4];
  1459. ssize_t ret = 0;
  1460. int len_off, err = -EIO;
  1461. while (s > 0) {
  1462. err = -EIO;
  1463. len_off = _picolcd_flash_setaddr(data, raw_data, *off);
  1464. raw_data[len_off] = s > 32 ? 32 : s;
  1465. resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off+1);
  1466. if (!resp || !resp->in_report)
  1467. goto skip;
  1468. if (resp->in_report->id == REPORT_MEMORY ||
  1469. resp->in_report->id == REPORT_BL_READ_MEMORY) {
  1470. if (memcmp(raw_data, resp->raw_data, len_off+1) != 0)
  1471. goto skip;
  1472. if (copy_to_user(u+ret, resp->raw_data+len_off+1, raw_data[len_off])) {
  1473. err = -EFAULT;
  1474. goto skip;
  1475. }
  1476. *off += raw_data[len_off];
  1477. s -= raw_data[len_off];
  1478. ret += raw_data[len_off];
  1479. err = 0;
  1480. }
  1481. skip:
  1482. kfree(resp);
  1483. if (err)
  1484. return ret > 0 ? ret : err;
  1485. }
  1486. return ret;
  1487. }
  1488. static ssize_t picolcd_debug_flash_read(struct file *f, char __user *u,
  1489. size_t s, loff_t *off)
  1490. {
  1491. struct picolcd_data *data = f->private_data;
  1492. if (s == 0)
  1493. return -EINVAL;
  1494. if (*off > 0x05fff)
  1495. return 0;
  1496. if (*off + s > 0x05fff)
  1497. s = 0x06000 - *off;
  1498. if (data->status & PICOLCD_BOOTLOADER)
  1499. return _picolcd_flash_read(data, REPORT_BL_READ_MEMORY, u, s, off);
  1500. else
  1501. return _picolcd_flash_read(data, REPORT_READ_MEMORY, u, s, off);
  1502. }
  1503. /* erase block aligned to 64bytes boundary */
  1504. static ssize_t _picolcd_flash_erase64(struct picolcd_data *data, int report_id,
  1505. loff_t *off)
  1506. {
  1507. struct picolcd_pending *resp;
  1508. u8 raw_data[3];
  1509. int len_off;
  1510. ssize_t ret = -EIO;
  1511. if (*off & 0x3f)
  1512. return -EINVAL;
  1513. len_off = _picolcd_flash_setaddr(data, raw_data, *off);
  1514. resp = picolcd_send_and_wait(data->hdev, report_id, raw_data, len_off);
  1515. if (!resp || !resp->in_report)
  1516. goto skip;
  1517. if (resp->in_report->id == REPORT_MEMORY ||
  1518. resp->in_report->id == REPORT_BL_ERASE_MEMORY) {
  1519. if (memcmp(raw_data, resp->raw_data, len_off) != 0)
  1520. goto skip;
  1521. ret = 0;
  1522. }
  1523. skip:
  1524. kfree(resp);
  1525. return ret;
  1526. }
  1527. /* write a given size of data (bounds check to be done by caller) */
  1528. static ssize_t _picolcd_flash_write(struct picolcd_data *data, int report_id,
  1529. const char __user *u, size_t s, loff_t *off)
  1530. {
  1531. struct picolcd_pending *resp;
  1532. u8 raw_data[36];
  1533. ssize_t ret = 0;
  1534. int len_off, err = -EIO;
  1535. while (s > 0) {
  1536. err = -EIO;
  1537. len_off = _picolcd_flash_setaddr(data, raw_data, *off);
  1538. raw_data[len_off] = s > 32 ? 32 : s;
  1539. if (copy_from_user(raw_data+len_off+1, u, raw_data[len_off])) {
  1540. err = -EFAULT;
  1541. break;
  1542. }
  1543. resp = picolcd_send_and_wait(data->hdev, report_id, raw_data,
  1544. len_off+1+raw_data[len_off]);
  1545. if (!resp || !resp->in_report)
  1546. goto skip;
  1547. if (resp->in_report->id == REPORT_MEMORY ||
  1548. resp->in_report->id == REPORT_BL_WRITE_MEMORY) {
  1549. if (memcmp(raw_data, resp->raw_data, len_off+1+raw_data[len_off]) != 0)
  1550. goto skip;
  1551. *off += raw_data[len_off];
  1552. s -= raw_data[len_off];
  1553. ret += raw_data[len_off];
  1554. err = 0;
  1555. }
  1556. skip:
  1557. kfree(resp);
  1558. if (err)
  1559. break;
  1560. }
  1561. return ret > 0 ? ret : err;
  1562. }
  1563. static ssize_t picolcd_debug_flash_write(struct file *f, const char __user *u,
  1564. size_t s, loff_t *off)
  1565. {
  1566. struct picolcd_data *data = f->private_data;
  1567. ssize_t err, ret = 0;
  1568. int report_erase, report_write;
  1569. if (s == 0)
  1570. return -EINVAL;
  1571. if (*off > 0x5fff)
  1572. return -ENOSPC;
  1573. if (s & 0x3f)
  1574. return -EINVAL;
  1575. if (*off & 0x3f)
  1576. return -EINVAL;
  1577. if (data->status & PICOLCD_BOOTLOADER) {
  1578. report_erase = REPORT_BL_ERASE_MEMORY;
  1579. report_write = REPORT_BL_WRITE_MEMORY;
  1580. } else {
  1581. report_erase = REPORT_ERASE_MEMORY;
  1582. report_write = REPORT_WRITE_MEMORY;
  1583. }
  1584. mutex_lock(&data->mutex_flash);
  1585. while (s > 0) {
  1586. err = _picolcd_flash_erase64(data, report_erase, off);
  1587. if (err)
  1588. break;
  1589. err = _picolcd_flash_write(data, report_write, u, 64, off);
  1590. if (err < 0)
  1591. break;
  1592. ret += err;
  1593. *off += err;
  1594. s -= err;
  1595. if (err != 64)
  1596. break;
  1597. }
  1598. mutex_unlock(&data->mutex_flash);
  1599. return ret > 0 ? ret : err;
  1600. }
  1601. /*
  1602. * Notes:
  1603. * - concurrent writing is prevented by mutex and all writes must be
  1604. * n*64 bytes and 64-byte aligned, each write being preceded by an
  1605. * ERASE which erases a 64byte block.
  1606. * If less than requested was written or an error is returned for an
  1607. * otherwise correct write request the next 64-byte block which should
  1608. * have been written is in undefined state (mostly: original, erased,
  1609. * (half-)written with write error)
  1610. * - reading can happen without special restriction
  1611. */
  1612. static const struct file_operations picolcd_debug_flash_fops = {
  1613. .owner = THIS_MODULE,
  1614. .open = simple_open,
  1615. .read = picolcd_debug_flash_read,
  1616. .write = picolcd_debug_flash_write,
  1617. .llseek = generic_file_llseek,
  1618. };
  1619. /*
  1620. * Helper code for HID report level dumping/debugging
  1621. */
  1622. static const char *error_codes[] = {
  1623. "success", "parameter missing", "data_missing", "block readonly",
  1624. "block not erasable", "block too big", "section overflow",
  1625. "invalid command length", "invalid data length",
  1626. };
  1627. static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data,
  1628. const size_t data_len)
  1629. {
  1630. int i, j;
  1631. for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) {
  1632. dst[j++] = hex_asc[(data[i] >> 4) & 0x0f];
  1633. dst[j++] = hex_asc[data[i] & 0x0f];
  1634. dst[j++] = ' ';
  1635. }
  1636. if (j < dst_sz) {
  1637. dst[j--] = '\0';
  1638. dst[j] = '\n';
  1639. } else
  1640. dst[j] = '\0';
  1641. }
  1642. static void picolcd_debug_out_report(struct picolcd_data *data,
  1643. struct hid_device *hdev, struct hid_report *report)
  1644. {
  1645. u8 raw_data[70];
  1646. int raw_size = (report->size >> 3) + 1;
  1647. char *buff;
  1648. #define BUFF_SZ 256
  1649. /* Avoid unnecessary overhead if debugfs is disabled */
  1650. if (!hdev->debug_events)
  1651. return;
  1652. buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
  1653. if (!buff)
  1654. return;
  1655. snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ",
  1656. report->id, raw_size);
  1657. hid_debug_event(hdev, buff);
  1658. if (raw_size + 5 > sizeof(raw_data)) {
  1659. kfree(buff);
  1660. hid_debug_event(hdev, " TOO BIG\n");
  1661. return;
  1662. } else {
  1663. raw_data[0] = report->id;
  1664. hid_output_report(report, raw_data);
  1665. dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size);
  1666. hid_debug_event(hdev, buff);
  1667. }
  1668. switch (report->id) {
  1669. case REPORT_LED_STATE:
  1670. /* 1 data byte with GPO state */
  1671. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1672. "REPORT_LED_STATE", report->id, raw_size-1);
  1673. hid_debug_event(hdev, buff);
  1674. snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]);
  1675. hid_debug_event(hdev, buff);
  1676. break;
  1677. case REPORT_BRIGHTNESS:
  1678. /* 1 data byte with brightness */
  1679. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1680. "REPORT_BRIGHTNESS", report->id, raw_size-1);
  1681. hid_debug_event(hdev, buff);
  1682. snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]);
  1683. hid_debug_event(hdev, buff);
  1684. break;
  1685. case REPORT_CONTRAST:
  1686. /* 1 data byte with contrast */
  1687. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1688. "REPORT_CONTRAST", report->id, raw_size-1);
  1689. hid_debug_event(hdev, buff);
  1690. snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]);
  1691. hid_debug_event(hdev, buff);
  1692. break;
  1693. case REPORT_RESET:
  1694. /* 2 data bytes with reset duration in ms */
  1695. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1696. "REPORT_RESET", report->id, raw_size-1);
  1697. hid_debug_event(hdev, buff);
  1698. snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n",
  1699. raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]);
  1700. hid_debug_event(hdev, buff);
  1701. break;
  1702. case REPORT_LCD_CMD:
  1703. /* 63 data bytes with LCD commands */
  1704. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1705. "REPORT_LCD_CMD", report->id, raw_size-1);
  1706. hid_debug_event(hdev, buff);
  1707. /* TODO: format decoding */
  1708. break;
  1709. case REPORT_LCD_DATA:
  1710. /* 63 data bytes with LCD data */
  1711. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1712. "REPORT_LCD_CMD", report->id, raw_size-1);
  1713. /* TODO: format decoding */
  1714. hid_debug_event(hdev, buff);
  1715. break;
  1716. case REPORT_LCD_CMD_DATA:
  1717. /* 63 data bytes with LCD commands and data */
  1718. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1719. "REPORT_LCD_CMD", report->id, raw_size-1);
  1720. /* TODO: format decoding */
  1721. hid_debug_event(hdev, buff);
  1722. break;
  1723. case REPORT_EE_READ:
  1724. /* 3 data bytes with read area description */
  1725. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1726. "REPORT_EE_READ", report->id, raw_size-1);
  1727. hid_debug_event(hdev, buff);
  1728. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
  1729. raw_data[2], raw_data[1]);
  1730. hid_debug_event(hdev, buff);
  1731. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
  1732. hid_debug_event(hdev, buff);
  1733. break;
  1734. case REPORT_EE_WRITE:
  1735. /* 3+1..20 data bytes with write area description */
  1736. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1737. "REPORT_EE_WRITE", report->id, raw_size-1);
  1738. hid_debug_event(hdev, buff);
  1739. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
  1740. raw_data[2], raw_data[1]);
  1741. hid_debug_event(hdev, buff);
  1742. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
  1743. hid_debug_event(hdev, buff);
  1744. if (raw_data[3] == 0) {
  1745. snprintf(buff, BUFF_SZ, "\tNo data\n");
  1746. } else if (raw_data[3] + 4 <= raw_size) {
  1747. snprintf(buff, BUFF_SZ, "\tData: ");
  1748. hid_debug_event(hdev, buff);
  1749. dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
  1750. } else {
  1751. snprintf(buff, BUFF_SZ, "\tData overflowed\n");
  1752. }
  1753. hid_debug_event(hdev, buff);
  1754. break;
  1755. case REPORT_ERASE_MEMORY:
  1756. case REPORT_BL_ERASE_MEMORY:
  1757. /* 3 data bytes with pointer inside erase block */
  1758. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1759. "REPORT_ERASE_MEMORY", report->id, raw_size-1);
  1760. hid_debug_event(hdev, buff);
  1761. switch (data->addr_sz) {
  1762. case 2:
  1763. snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n",
  1764. raw_data[2], raw_data[1]);
  1765. break;
  1766. case 3:
  1767. snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n",
  1768. raw_data[3], raw_data[2], raw_data[1]);
  1769. break;
  1770. default:
  1771. snprintf(buff, BUFF_SZ, "\tNot supported\n");
  1772. }
  1773. hid_debug_event(hdev, buff);
  1774. break;
  1775. case REPORT_READ_MEMORY:
  1776. case REPORT_BL_READ_MEMORY:
  1777. /* 4 data bytes with read area description */
  1778. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1779. "REPORT_READ_MEMORY", report->id, raw_size-1);
  1780. hid_debug_event(hdev, buff);
  1781. switch (data->addr_sz) {
  1782. case 2:
  1783. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
  1784. raw_data[2], raw_data[1]);
  1785. hid_debug_event(hdev, buff);
  1786. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
  1787. break;
  1788. case 3:
  1789. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
  1790. raw_data[3], raw_data[2], raw_data[1]);
  1791. hid_debug_event(hdev, buff);
  1792. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
  1793. break;
  1794. default:
  1795. snprintf(buff, BUFF_SZ, "\tNot supported\n");
  1796. }
  1797. hid_debug_event(hdev, buff);
  1798. break;
  1799. case REPORT_WRITE_MEMORY:
  1800. case REPORT_BL_WRITE_MEMORY:
  1801. /* 4+1..32 data bytes with write adrea description */
  1802. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1803. "REPORT_WRITE_MEMORY", report->id, raw_size-1);
  1804. hid_debug_event(hdev, buff);
  1805. switch (data->addr_sz) {
  1806. case 2:
  1807. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
  1808. raw_data[2], raw_data[1]);
  1809. hid_debug_event(hdev, buff);
  1810. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
  1811. hid_debug_event(hdev, buff);
  1812. if (raw_data[3] == 0) {
  1813. snprintf(buff, BUFF_SZ, "\tNo data\n");
  1814. } else if (raw_data[3] + 4 <= raw_size) {
  1815. snprintf(buff, BUFF_SZ, "\tData: ");
  1816. hid_debug_event(hdev, buff);
  1817. dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
  1818. } else {
  1819. snprintf(buff, BUFF_SZ, "\tData overflowed\n");
  1820. }
  1821. break;
  1822. case 3:
  1823. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
  1824. raw_data[3], raw_data[2], raw_data[1]);
  1825. hid_debug_event(hdev, buff);
  1826. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
  1827. hid_debug_event(hdev, buff);
  1828. if (raw_data[4] == 0) {
  1829. snprintf(buff, BUFF_SZ, "\tNo data\n");
  1830. } else if (raw_data[4] + 5 <= raw_size) {
  1831. snprintf(buff, BUFF_SZ, "\tData: ");
  1832. hid_debug_event(hdev, buff);
  1833. dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
  1834. } else {
  1835. snprintf(buff, BUFF_SZ, "\tData overflowed\n");
  1836. }
  1837. break;
  1838. default:
  1839. snprintf(buff, BUFF_SZ, "\tNot supported\n");
  1840. }
  1841. hid_debug_event(hdev, buff);
  1842. break;
  1843. case REPORT_SPLASH_RESTART:
  1844. /* TODO */
  1845. break;
  1846. case REPORT_EXIT_KEYBOARD:
  1847. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1848. "REPORT_EXIT_KEYBOARD", report->id, raw_size-1);
  1849. hid_debug_event(hdev, buff);
  1850. snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
  1851. raw_data[1] | (raw_data[2] << 8),
  1852. raw_data[2], raw_data[1]);
  1853. hid_debug_event(hdev, buff);
  1854. break;
  1855. case REPORT_VERSION:
  1856. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1857. "REPORT_VERSION", report->id, raw_size-1);
  1858. hid_debug_event(hdev, buff);
  1859. break;
  1860. case REPORT_DEVID:
  1861. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1862. "REPORT_DEVID", report->id, raw_size-1);
  1863. hid_debug_event(hdev, buff);
  1864. break;
  1865. case REPORT_SPLASH_SIZE:
  1866. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1867. "REPORT_SPLASH_SIZE", report->id, raw_size-1);
  1868. hid_debug_event(hdev, buff);
  1869. break;
  1870. case REPORT_HOOK_VERSION:
  1871. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1872. "REPORT_HOOK_VERSION", report->id, raw_size-1);
  1873. hid_debug_event(hdev, buff);
  1874. break;
  1875. case REPORT_EXIT_FLASHER:
  1876. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1877. "REPORT_VERSION", report->id, raw_size-1);
  1878. hid_debug_event(hdev, buff);
  1879. snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n",
  1880. raw_data[1] | (raw_data[2] << 8),
  1881. raw_data[2], raw_data[1]);
  1882. hid_debug_event(hdev, buff);
  1883. break;
  1884. default:
  1885. snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n",
  1886. "<unknown>", report->id, raw_size-1);
  1887. hid_debug_event(hdev, buff);
  1888. break;
  1889. }
  1890. wake_up_interruptible(&hdev->debug_wait);
  1891. kfree(buff);
  1892. }
  1893. static void picolcd_debug_raw_event(struct picolcd_data *data,
  1894. struct hid_device *hdev, struct hid_report *report,
  1895. u8 *raw_data, int size)
  1896. {
  1897. char *buff;
  1898. #define BUFF_SZ 256
  1899. /* Avoid unnecessary overhead if debugfs is disabled */
  1900. if (!hdev->debug_events)
  1901. return;
  1902. buff = kmalloc(BUFF_SZ, GFP_ATOMIC);
  1903. if (!buff)
  1904. return;
  1905. switch (report->id) {
  1906. case REPORT_ERROR_CODE:
  1907. /* 2 data bytes with affected report and error code */
  1908. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  1909. "REPORT_ERROR_CODE", report->id, size-1);
  1910. hid_debug_event(hdev, buff);
  1911. if (raw_data[2] < ARRAY_SIZE(error_codes))
  1912. snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n",
  1913. raw_data[2], error_codes[raw_data[2]], raw_data[1]);
  1914. else
  1915. snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n",
  1916. raw_data[2], raw_data[1]);
  1917. hid_debug_event(hdev, buff);
  1918. break;
  1919. case REPORT_KEY_STATE:
  1920. /* 2 data bytes with key state */
  1921. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  1922. "REPORT_KEY_STATE", report->id, size-1);
  1923. hid_debug_event(hdev, buff);
  1924. if (raw_data[1] == 0)
  1925. snprintf(buff, BUFF_SZ, "\tNo key pressed\n");
  1926. else if (raw_data[2] == 0)
  1927. snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n",
  1928. raw_data[1], raw_data[1]);
  1929. else
  1930. snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n",
  1931. raw_data[1], raw_data[1], raw_data[2], raw_data[2]);
  1932. hid_debug_event(hdev, buff);
  1933. break;
  1934. case REPORT_IR_DATA:
  1935. /* Up to 20 byes of IR scancode data */
  1936. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  1937. "REPORT_IR_DATA", report->id, size-1);
  1938. hid_debug_event(hdev, buff);
  1939. if (raw_data[1] == 0) {
  1940. snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n");
  1941. hid_debug_event(hdev, buff);
  1942. } else if (raw_data[1] + 1 <= size) {
  1943. snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ",
  1944. raw_data[1]-1);
  1945. hid_debug_event(hdev, buff);
  1946. dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1);
  1947. hid_debug_event(hdev, buff);
  1948. } else {
  1949. snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n",
  1950. raw_data[1]-1);
  1951. hid_debug_event(hdev, buff);
  1952. }
  1953. break;
  1954. case REPORT_EE_DATA:
  1955. /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */
  1956. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  1957. "REPORT_EE_DATA", report->id, size-1);
  1958. hid_debug_event(hdev, buff);
  1959. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
  1960. raw_data[2], raw_data[1]);
  1961. hid_debug_event(hdev, buff);
  1962. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
  1963. hid_debug_event(hdev, buff);
  1964. if (raw_data[3] == 0) {
  1965. snprintf(buff, BUFF_SZ, "\tNo data\n");
  1966. hid_debug_event(hdev, buff);
  1967. } else if (raw_data[3] + 4 <= size) {
  1968. snprintf(buff, BUFF_SZ, "\tData: ");
  1969. hid_debug_event(hdev, buff);
  1970. dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
  1971. hid_debug_event(hdev, buff);
  1972. } else {
  1973. snprintf(buff, BUFF_SZ, "\tData overflowed\n");
  1974. hid_debug_event(hdev, buff);
  1975. }
  1976. break;
  1977. case REPORT_MEMORY:
  1978. /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */
  1979. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  1980. "REPORT_MEMORY", report->id, size-1);
  1981. hid_debug_event(hdev, buff);
  1982. switch (data->addr_sz) {
  1983. case 2:
  1984. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n",
  1985. raw_data[2], raw_data[1]);
  1986. hid_debug_event(hdev, buff);
  1987. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]);
  1988. hid_debug_event(hdev, buff);
  1989. if (raw_data[3] == 0) {
  1990. snprintf(buff, BUFF_SZ, "\tNo data\n");
  1991. } else if (raw_data[3] + 4 <= size) {
  1992. snprintf(buff, BUFF_SZ, "\tData: ");
  1993. hid_debug_event(hdev, buff);
  1994. dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]);
  1995. } else {
  1996. snprintf(buff, BUFF_SZ, "\tData overflowed\n");
  1997. }
  1998. break;
  1999. case 3:
  2000. snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n",
  2001. raw_data[3], raw_data[2], raw_data[1]);
  2002. hid_debug_event(hdev, buff);
  2003. snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]);
  2004. hid_debug_event(hdev, buff);
  2005. if (raw_data[4] == 0) {
  2006. snprintf(buff, BUFF_SZ, "\tNo data\n");
  2007. } else if (raw_data[4] + 5 <= size) {
  2008. snprintf(buff, BUFF_SZ, "\tData: ");
  2009. hid_debug_event(hdev, buff);
  2010. dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]);
  2011. } else {
  2012. snprintf(buff, BUFF_SZ, "\tData overflowed\n");
  2013. }
  2014. break;
  2015. default:
  2016. snprintf(buff, BUFF_SZ, "\tNot supported\n");
  2017. }
  2018. hid_debug_event(hdev, buff);
  2019. break;
  2020. case REPORT_VERSION:
  2021. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2022. "REPORT_VERSION", report->id, size-1);
  2023. hid_debug_event(hdev, buff);
  2024. snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
  2025. raw_data[2], raw_data[1]);
  2026. hid_debug_event(hdev, buff);
  2027. break;
  2028. case REPORT_BL_ERASE_MEMORY:
  2029. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2030. "REPORT_BL_ERASE_MEMORY", report->id, size-1);
  2031. hid_debug_event(hdev, buff);
  2032. /* TODO */
  2033. break;
  2034. case REPORT_BL_READ_MEMORY:
  2035. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2036. "REPORT_BL_READ_MEMORY", report->id, size-1);
  2037. hid_debug_event(hdev, buff);
  2038. /* TODO */
  2039. break;
  2040. case REPORT_BL_WRITE_MEMORY:
  2041. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2042. "REPORT_BL_WRITE_MEMORY", report->id, size-1);
  2043. hid_debug_event(hdev, buff);
  2044. /* TODO */
  2045. break;
  2046. case REPORT_DEVID:
  2047. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2048. "REPORT_DEVID", report->id, size-1);
  2049. hid_debug_event(hdev, buff);
  2050. snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n",
  2051. raw_data[1], raw_data[2], raw_data[3], raw_data[4]);
  2052. hid_debug_event(hdev, buff);
  2053. snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n",
  2054. raw_data[5]);
  2055. hid_debug_event(hdev, buff);
  2056. break;
  2057. case REPORT_SPLASH_SIZE:
  2058. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2059. "REPORT_SPLASH_SIZE", report->id, size-1);
  2060. hid_debug_event(hdev, buff);
  2061. snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n",
  2062. (raw_data[2] << 8) | raw_data[1]);
  2063. hid_debug_event(hdev, buff);
  2064. snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n",
  2065. (raw_data[4] << 8) | raw_data[3]);
  2066. hid_debug_event(hdev, buff);
  2067. break;
  2068. case REPORT_HOOK_VERSION:
  2069. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2070. "REPORT_HOOK_VERSION", report->id, size-1);
  2071. hid_debug_event(hdev, buff);
  2072. snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n",
  2073. raw_data[1], raw_data[2]);
  2074. hid_debug_event(hdev, buff);
  2075. break;
  2076. default:
  2077. snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n",
  2078. "<unknown>", report->id, size-1);
  2079. hid_debug_event(hdev, buff);
  2080. break;
  2081. }
  2082. wake_up_interruptible(&hdev->debug_wait);
  2083. kfree(buff);
  2084. }
  2085. static void picolcd_init_devfs(struct picolcd_data *data,
  2086. struct hid_report *eeprom_r, struct hid_report *eeprom_w,
  2087. struct hid_report *flash_r, struct hid_report *flash_w,
  2088. struct hid_report *reset)
  2089. {
  2090. struct hid_device *hdev = data->hdev;
  2091. mutex_init(&data->mutex_flash);
  2092. /* reset */
  2093. if (reset)
  2094. data->debug_reset = debugfs_create_file("reset", 0600,
  2095. hdev->debug_dir, data, &picolcd_debug_reset_fops);
  2096. /* eeprom */
  2097. if (eeprom_r || eeprom_w)
  2098. data->debug_eeprom = debugfs_create_file("eeprom",
  2099. (eeprom_w ? S_IWUSR : 0) | (eeprom_r ? S_IRUSR : 0),
  2100. hdev->debug_dir, data, &picolcd_debug_eeprom_fops);
  2101. /* flash */
  2102. if (flash_r && flash_r->maxfield == 1 && flash_r->field[0]->report_size == 8)
  2103. data->addr_sz = flash_r->field[0]->report_count - 1;
  2104. else
  2105. data->addr_sz = -1;
  2106. if (data->addr_sz == 2 || data->addr_sz == 3) {
  2107. data->debug_flash = debugfs_create_file("flash",
  2108. (flash_w ? S_IWUSR : 0) | (flash_r ? S_IRUSR : 0),
  2109. hdev->debug_dir, data, &picolcd_debug_flash_fops);
  2110. } else if (flash_r || flash_w)
  2111. hid_warn(hdev, "Unexpected FLASH access reports, please submit rdesc for review\n");
  2112. }
  2113. static void picolcd_exit_devfs(struct picolcd_data *data)
  2114. {
  2115. struct dentry *dent;
  2116. dent = data->debug_reset;
  2117. data->debug_reset = NULL;
  2118. if (dent)
  2119. debugfs_remove(dent);
  2120. dent = data->debug_eeprom;
  2121. data->debug_eeprom = NULL;
  2122. if (dent)
  2123. debugfs_remove(dent);
  2124. dent = data->debug_flash;
  2125. data->debug_flash = NULL;
  2126. if (dent)
  2127. debugfs_remove(dent);
  2128. mutex_destroy(&data->mutex_flash);
  2129. }
  2130. #else
  2131. static inline void picolcd_debug_raw_event(struct picolcd_data *data,
  2132. struct hid_device *hdev, struct hid_report *report,
  2133. u8 *raw_data, int size)
  2134. {
  2135. }
  2136. static inline void picolcd_init_devfs(struct picolcd_data *data,
  2137. struct hid_report *eeprom_r, struct hid_report *eeprom_w,
  2138. struct hid_report *flash_r, struct hid_report *flash_w,
  2139. struct hid_report *reset)
  2140. {
  2141. }
  2142. static inline void picolcd_exit_devfs(struct picolcd_data *data)
  2143. {
  2144. }
  2145. #endif /* CONFIG_DEBUG_FS */
  2146. /*
  2147. * Handle raw report as sent by device
  2148. */
  2149. static int picolcd_raw_event(struct hid_device *hdev,
  2150. struct hid_report *report, u8 *raw_data, int size)
  2151. {
  2152. struct picolcd_data *data = hid_get_drvdata(hdev);
  2153. unsigned long flags;
  2154. int ret = 0;
  2155. if (!data)
  2156. return 1;
  2157. if (size > 64) {
  2158. hid_warn(hdev, "invalid size value(%d) for picolcd raw event\n",
  2159. size);
  2160. return 0;
  2161. }
  2162. if (report->id == REPORT_KEY_STATE) {
  2163. if (data->input_keys)
  2164. ret = picolcd_raw_keypad(data, report, raw_data+1, size-1);
  2165. } else if (report->id == REPORT_IR_DATA) {
  2166. if (data->input_cir)
  2167. ret = picolcd_raw_cir(data, report, raw_data+1, size-1);
  2168. } else {
  2169. spin_lock_irqsave(&data->lock, flags);
  2170. /*
  2171. * We let the caller of picolcd_send_and_wait() check if the
  2172. * report we got is one of the expected ones or not.
  2173. */
  2174. if (data->pending) {
  2175. memcpy(data->pending->raw_data, raw_data+1, size-1);
  2176. data->pending->raw_size = size-1;
  2177. data->pending->in_report = report;
  2178. complete(&data->pending->ready);
  2179. }
  2180. spin_unlock_irqrestore(&data->lock, flags);
  2181. }
  2182. picolcd_debug_raw_event(data, hdev, report, raw_data, size);
  2183. return 1;
  2184. }
  2185. #ifdef CONFIG_PM
  2186. static int picolcd_suspend(struct hid_device *hdev, pm_message_t message)
  2187. {
  2188. if (PMSG_IS_AUTO(message))
  2189. return 0;
  2190. picolcd_suspend_backlight(hid_get_drvdata(hdev));
  2191. dbg_hid(PICOLCD_NAME " device ready for suspend\n");
  2192. return 0;
  2193. }
  2194. static int picolcd_resume(struct hid_device *hdev)
  2195. {
  2196. int ret;
  2197. ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
  2198. if (ret)
  2199. dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
  2200. return 0;
  2201. }
  2202. static int picolcd_reset_resume(struct hid_device *hdev)
  2203. {
  2204. int ret;
  2205. ret = picolcd_reset(hdev);
  2206. if (ret)
  2207. dbg_hid(PICOLCD_NAME " resetting our device failed: %d\n", ret);
  2208. ret = picolcd_fb_reset(hid_get_drvdata(hdev), 0);
  2209. if (ret)
  2210. dbg_hid(PICOLCD_NAME " restoring framebuffer content failed: %d\n", ret);
  2211. ret = picolcd_resume_lcd(hid_get_drvdata(hdev));
  2212. if (ret)
  2213. dbg_hid(PICOLCD_NAME " restoring lcd failed: %d\n", ret);
  2214. ret = picolcd_resume_backlight(hid_get_drvdata(hdev));
  2215. if (ret)
  2216. dbg_hid(PICOLCD_NAME " restoring backlight failed: %d\n", ret);
  2217. picolcd_leds_set(hid_get_drvdata(hdev));
  2218. return 0;
  2219. }
  2220. #endif
  2221. /* initialize keypad input device */
  2222. static int picolcd_init_keys(struct picolcd_data *data,
  2223. struct hid_report *report)
  2224. {
  2225. struct hid_device *hdev = data->hdev;
  2226. struct input_dev *idev;
  2227. int error, i;
  2228. if (!report)
  2229. return -ENODEV;
  2230. if (report->maxfield != 1 || report->field[0]->report_count != 2 ||
  2231. report->field[0]->report_size != 8) {
  2232. hid_err(hdev, "unsupported KEY_STATE report\n");
  2233. return -EINVAL;
  2234. }
  2235. idev = input_allocate_device();
  2236. if (idev == NULL) {
  2237. hid_err(hdev, "failed to allocate input device\n");
  2238. return -ENOMEM;
  2239. }
  2240. input_set_drvdata(idev, hdev);
  2241. memcpy(data->keycode, def_keymap, sizeof(def_keymap));
  2242. idev->name = hdev->name;
  2243. idev->phys = hdev->phys;
  2244. idev->uniq = hdev->uniq;
  2245. idev->id.bustype = hdev->bus;
  2246. idev->id.vendor = hdev->vendor;
  2247. idev->id.product = hdev->product;
  2248. idev->id.version = hdev->version;
  2249. idev->dev.parent = hdev->dev.parent;
  2250. idev->keycode = &data->keycode;
  2251. idev->keycodemax = PICOLCD_KEYS;
  2252. idev->keycodesize = sizeof(data->keycode[0]);
  2253. input_set_capability(idev, EV_MSC, MSC_SCAN);
  2254. set_bit(EV_REP, idev->evbit);
  2255. for (i = 0; i < PICOLCD_KEYS; i++)
  2256. input_set_capability(idev, EV_KEY, data->keycode[i]);
  2257. error = input_register_device(idev);
  2258. if (error) {
  2259. hid_err(hdev, "error registering the input device\n");
  2260. input_free_device(idev);
  2261. return error;
  2262. }
  2263. data->input_keys = idev;
  2264. return 0;
  2265. }
  2266. static void picolcd_exit_keys(struct picolcd_data *data)
  2267. {
  2268. struct input_dev *idev = data->input_keys;
  2269. data->input_keys = NULL;
  2270. if (idev)
  2271. input_unregister_device(idev);
  2272. }
  2273. /* initialize CIR input device */
  2274. static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report)
  2275. {
  2276. /* support not implemented yet */
  2277. return 0;
  2278. }
  2279. static inline void picolcd_exit_cir(struct picolcd_data *data)
  2280. {
  2281. }
  2282. static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data)
  2283. {
  2284. int error;
  2285. error = picolcd_check_version(hdev);
  2286. if (error)
  2287. return error;
  2288. if (data->version[0] != 0 && data->version[1] != 3)
  2289. hid_info(hdev, "Device with untested firmware revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
  2290. dev_name(&hdev->dev));
  2291. /* Setup keypad input device */
  2292. error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev));
  2293. if (error)
  2294. goto err;
  2295. /* Setup CIR input device */
  2296. error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev));
  2297. if (error)
  2298. goto err;
  2299. /* Set up the framebuffer device */
  2300. error = picolcd_init_framebuffer(data);
  2301. if (error)
  2302. goto err;
  2303. /* Setup lcd class device */
  2304. error = picolcd_init_lcd(data, picolcd_out_report(REPORT_CONTRAST, hdev));
  2305. if (error)
  2306. goto err;
  2307. /* Setup backlight class device */
  2308. error = picolcd_init_backlight(data, picolcd_out_report(REPORT_BRIGHTNESS, hdev));
  2309. if (error)
  2310. goto err;
  2311. /* Setup the LED class devices */
  2312. error = picolcd_init_leds(data, picolcd_out_report(REPORT_LED_STATE, hdev));
  2313. if (error)
  2314. goto err;
  2315. picolcd_init_devfs(data, picolcd_out_report(REPORT_EE_READ, hdev),
  2316. picolcd_out_report(REPORT_EE_WRITE, hdev),
  2317. picolcd_out_report(REPORT_READ_MEMORY, hdev),
  2318. picolcd_out_report(REPORT_WRITE_MEMORY, hdev),
  2319. picolcd_out_report(REPORT_RESET, hdev));
  2320. return 0;
  2321. err:
  2322. picolcd_exit_leds(data);
  2323. picolcd_exit_backlight(data);
  2324. picolcd_exit_lcd(data);
  2325. picolcd_exit_framebuffer(data);
  2326. picolcd_exit_cir(data);
  2327. picolcd_exit_keys(data);
  2328. return error;
  2329. }
  2330. static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data)
  2331. {
  2332. int error;
  2333. error = picolcd_check_version(hdev);
  2334. if (error)
  2335. return error;
  2336. if (data->version[0] != 1 && data->version[1] != 0)
  2337. hid_info(hdev, "Device with untested bootloader revision, please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n",
  2338. dev_name(&hdev->dev));
  2339. picolcd_init_devfs(data, NULL, NULL,
  2340. picolcd_out_report(REPORT_BL_READ_MEMORY, hdev),
  2341. picolcd_out_report(REPORT_BL_WRITE_MEMORY, hdev), NULL);
  2342. return 0;
  2343. }
  2344. static int picolcd_probe(struct hid_device *hdev,
  2345. const struct hid_device_id *id)
  2346. {
  2347. struct picolcd_data *data;
  2348. int error = -ENOMEM;
  2349. dbg_hid(PICOLCD_NAME " hardware probe...\n");
  2350. /*
  2351. * Let's allocate the picolcd data structure, set some reasonable
  2352. * defaults, and associate it with the device
  2353. */
  2354. data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL);
  2355. if (data == NULL) {
  2356. hid_err(hdev, "can't allocate space for Minibox PicoLCD device data\n");
  2357. error = -ENOMEM;
  2358. goto err_no_cleanup;
  2359. }
  2360. spin_lock_init(&data->lock);
  2361. mutex_init(&data->mutex);
  2362. data->hdev = hdev;
  2363. data->opmode_delay = 5000;
  2364. if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER)
  2365. data->status |= PICOLCD_BOOTLOADER;
  2366. hid_set_drvdata(hdev, data);
  2367. /* Parse the device reports and start it up */
  2368. error = hid_parse(hdev);
  2369. if (error) {
  2370. hid_err(hdev, "device report parse failed\n");
  2371. goto err_cleanup_data;
  2372. }
  2373. /* We don't use hidinput but hid_hw_start() fails if nothing is
  2374. * claimed. So spoof claimed input. */
  2375. hdev->claimed = HID_CLAIMED_INPUT;
  2376. error = hid_hw_start(hdev, 0);
  2377. hdev->claimed = 0;
  2378. if (error) {
  2379. hid_err(hdev, "hardware start failed\n");
  2380. goto err_cleanup_data;
  2381. }
  2382. error = hid_hw_open(hdev);
  2383. if (error) {
  2384. hid_err(hdev, "failed to open input interrupt pipe for key and IR events\n");
  2385. goto err_cleanup_hid_hw;
  2386. }
  2387. error = device_create_file(&hdev->dev, &dev_attr_operation_mode_delay);
  2388. if (error) {
  2389. hid_err(hdev, "failed to create sysfs attributes\n");
  2390. goto err_cleanup_hid_ll;
  2391. }
  2392. error = device_create_file(&hdev->dev, &dev_attr_operation_mode);
  2393. if (error) {
  2394. hid_err(hdev, "failed to create sysfs attributes\n");
  2395. goto err_cleanup_sysfs1;
  2396. }
  2397. if (data->status & PICOLCD_BOOTLOADER)
  2398. error = picolcd_probe_bootloader(hdev, data);
  2399. else
  2400. error = picolcd_probe_lcd(hdev, data);
  2401. if (error)
  2402. goto err_cleanup_sysfs2;
  2403. dbg_hid(PICOLCD_NAME " activated and initialized\n");
  2404. return 0;
  2405. err_cleanup_sysfs2:
  2406. device_remove_file(&hdev->dev, &dev_attr_operation_mode);
  2407. err_cleanup_sysfs1:
  2408. device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
  2409. err_cleanup_hid_ll:
  2410. hid_hw_close(hdev);
  2411. err_cleanup_hid_hw:
  2412. hid_hw_stop(hdev);
  2413. err_cleanup_data:
  2414. kfree(data);
  2415. err_no_cleanup:
  2416. hid_set_drvdata(hdev, NULL);
  2417. return error;
  2418. }
  2419. static void picolcd_remove(struct hid_device *hdev)
  2420. {
  2421. struct picolcd_data *data = hid_get_drvdata(hdev);
  2422. unsigned long flags;
  2423. dbg_hid(PICOLCD_NAME " hardware remove...\n");
  2424. spin_lock_irqsave(&data->lock, flags);
  2425. data->status |= PICOLCD_FAILED;
  2426. spin_unlock_irqrestore(&data->lock, flags);
  2427. #ifdef CONFIG_HID_PICOLCD_FB
  2428. /* short-circuit FB as early as possible in order to
  2429. * avoid long delays if we host console.
  2430. */
  2431. if (data->fb_info)
  2432. data->fb_info->par = NULL;
  2433. #endif
  2434. picolcd_exit_devfs(data);
  2435. device_remove_file(&hdev->dev, &dev_attr_operation_mode);
  2436. device_remove_file(&hdev->dev, &dev_attr_operation_mode_delay);
  2437. hid_hw_close(hdev);
  2438. hid_hw_stop(hdev);
  2439. hid_set_drvdata(hdev, NULL);
  2440. /* Shortcut potential pending reply that will never arrive */
  2441. spin_lock_irqsave(&data->lock, flags);
  2442. if (data->pending)
  2443. complete(&data->pending->ready);
  2444. spin_unlock_irqrestore(&data->lock, flags);
  2445. /* Cleanup LED */
  2446. picolcd_exit_leds(data);
  2447. /* Clean up the framebuffer */
  2448. picolcd_exit_backlight(data);
  2449. picolcd_exit_lcd(data);
  2450. picolcd_exit_framebuffer(data);
  2451. /* Cleanup input */
  2452. picolcd_exit_cir(data);
  2453. picolcd_exit_keys(data);
  2454. mutex_destroy(&data->mutex);
  2455. /* Finally, clean up the picolcd data itself */
  2456. kfree(data);
  2457. }
  2458. static const struct hid_device_id picolcd_devices[] = {
  2459. { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) },
  2460. { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) },
  2461. { }
  2462. };
  2463. MODULE_DEVICE_TABLE(hid, picolcd_devices);
  2464. static struct hid_driver picolcd_driver = {
  2465. .name = "hid-picolcd",
  2466. .id_table = picolcd_devices,
  2467. .probe = picolcd_probe,
  2468. .remove = picolcd_remove,
  2469. .raw_event = picolcd_raw_event,
  2470. #ifdef CONFIG_PM
  2471. .suspend = picolcd_suspend,
  2472. .resume = picolcd_resume,
  2473. .reset_resume = picolcd_reset_resume,
  2474. #endif
  2475. };
  2476. static int __init picolcd_init(void)
  2477. {
  2478. return hid_register_driver(&picolcd_driver);
  2479. }
  2480. static void __exit picolcd_exit(void)
  2481. {
  2482. hid_unregister_driver(&picolcd_driver);
  2483. #ifdef CONFIG_HID_PICOLCD_FB
  2484. flush_work_sync(&picolcd_fb_cleanup);
  2485. WARN_ON(fb_pending);
  2486. #endif
  2487. }
  2488. module_init(picolcd_init);
  2489. module_exit(picolcd_exit);
  2490. MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver");
  2491. MODULE_LICENSE("GPL v2");