usf.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/compat.h>
  6. #include <linux/fs.h>
  7. #include <linux/module.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/wait.h>
  12. #include <linux/input.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/time.h>
  15. #include <linux/kmemleak.h>
  16. #include <linux/mutex.h>
  17. #include <dsp/apr_audio-v2.h>
  18. #include "q6usm.h"
  19. #include "usf.h"
  20. #include "usfcdev.h"
  21. #include "q6_init.h"
  22. /* The driver version*/
  23. #define DRV_VERSION "1.7.1"
  24. #define USF_VERSION_ID 0x0171
  25. /* Standard timeout in the asynchronous ops */
  26. #define USF_TIMEOUT_JIFFIES (1*HZ) /* 1 sec */
  27. /* Undefined USF device */
  28. #define USF_UNDEF_DEV_ID 0xffff
  29. /* TX memory mapping flag */
  30. #define USF_VM_READ 1
  31. /* RX memory mapping flag */
  32. #define USF_VM_WRITE 2
  33. /* Number of events, copied from the user space to kernel one */
  34. #define USF_EVENTS_PORTION_SIZE 20
  35. /* Indexes in range definitions */
  36. #define MIN_IND 0
  37. #define MAX_IND 1
  38. /* The coordinates indexes */
  39. #define X_IND 0
  40. #define Y_IND 1
  41. #define Z_IND 2
  42. /* Shared memory limits */
  43. /* max_buf_size = (port_size(65535*2) * port_num(8) * group_size(3) */
  44. #define USF_MAX_BUF_SIZE 3145680
  45. #define USF_MAX_BUF_NUM 32
  46. /* max size for buffer set from user space */
  47. #define USF_MAX_USER_BUF_SIZE 100000
  48. /* Place for opreation result, received from QDSP6 */
  49. #define APR_RESULT_IND 1
  50. /* Place for US detection result, received from QDSP6 */
  51. #define APR_US_DETECT_RESULT_IND 0
  52. #define BITS_IN_BYTE 8
  53. /* Time to stay awake after tx read event (e.g., proximity) */
  54. #define STAY_AWAKE_AFTER_READ_MSECS 3000
  55. /* The driver states */
  56. enum usf_state_type {
  57. USF_IDLE_STATE,
  58. USF_OPENED_STATE,
  59. USF_CONFIGURED_STATE,
  60. USF_WORK_STATE,
  61. USF_ADSP_RESTART_STATE,
  62. USF_ERROR_STATE
  63. };
  64. /* The US detection status upon FW/HW based US detection results */
  65. enum usf_us_detect_type {
  66. USF_US_DETECT_UNDEF,
  67. USF_US_DETECT_YES,
  68. USF_US_DETECT_NO
  69. };
  70. struct usf_xx_type {
  71. /* Name of the client - event calculator */
  72. char client_name[USF_MAX_CLIENT_NAME_SIZE];
  73. /* The driver state in TX or RX direction */
  74. enum usf_state_type usf_state;
  75. /* wait for q6 events mechanism */
  76. wait_queue_head_t wait;
  77. /* IF with q6usm info */
  78. struct us_client *usc;
  79. /* Q6:USM' Encoder/decoder configuration */
  80. struct us_encdec_cfg encdec_cfg;
  81. /* Shared buffer (with Q6:USM) size */
  82. uint32_t buffer_size;
  83. /* Number of the shared buffers (with Q6:USM) */
  84. uint32_t buffer_count;
  85. /* Shared memory (Cyclic buffer with 1 gap) control */
  86. uint32_t new_region;
  87. uint32_t prev_region;
  88. /* Q6:USM's events handler */
  89. void (*cb)(uint32_t, uint32_t, uint32_t *, void *);
  90. /* US detection result */
  91. enum usf_us_detect_type us_detect_type;
  92. /* User's update info isn't acceptable */
  93. u8 user_upd_info_na;
  94. };
  95. struct usf_type {
  96. /* TX device component configuration & control */
  97. struct usf_xx_type usf_tx;
  98. /* RX device component configuration & control */
  99. struct usf_xx_type usf_rx;
  100. /* Index into the opened device container */
  101. /* To prevent mutual usage of the same device */
  102. uint16_t dev_ind;
  103. /* Event types, supported by device */
  104. uint16_t event_types;
  105. /* The input devices are "input" module registered clients */
  106. struct input_dev *input_ifs[USF_MAX_EVENT_IND];
  107. /* Bitmap of types of events, conflicting to USF's ones */
  108. uint16_t conflicting_event_types;
  109. /* Bitmap of types of events from devs, conflicting with USF */
  110. uint16_t conflicting_event_filters;
  111. /* The requested buttons bitmap */
  112. uint16_t req_buttons_bitmap;
  113. /* Mutex for exclusive operations (all public APIs) */
  114. struct mutex mutex;
  115. };
  116. struct usf_input_dev_type {
  117. /* Input event type, supported by the input device */
  118. uint16_t event_type;
  119. /* Input device name */
  120. const char *input_dev_name;
  121. /* Input device registration function */
  122. int (*prepare_dev)(uint16_t, struct usf_type *,
  123. struct us_input_info_type *,
  124. const char *);
  125. /* Input event notification function */
  126. void (*notify_event)(struct usf_type *,
  127. uint16_t,
  128. struct usf_event_type *
  129. );
  130. };
  131. /* The MAX number of the supported devices */
  132. #define MAX_DEVS_NUMBER 1
  133. /*
  134. * code for a special button that is used to show/hide a
  135. * hovering cursor in the input framework. Must be in
  136. * sync with the button code definition in the framework
  137. * (EventHub.h)
  138. */
  139. #define BTN_USF_HOVERING_CURSOR 0x230
  140. /* Supported buttons container */
  141. static const int s_button_map[] = {
  142. BTN_STYLUS,
  143. BTN_STYLUS2,
  144. BTN_TOOL_PEN,
  145. BTN_TOOL_RUBBER,
  146. BTN_TOOL_FINGER,
  147. BTN_USF_HOVERING_CURSOR
  148. };
  149. /* The opened devices container */
  150. static atomic_t s_opened_devs[MAX_DEVS_NUMBER];
  151. static struct wakeup_source usf_wakeup_source;
  152. #define USF_NAME_PREFIX "usf_"
  153. #define USF_NAME_PREFIX_SIZE 4
  154. static struct input_dev *allocate_dev(uint16_t ind, const char *name)
  155. {
  156. struct input_dev *in_dev = input_allocate_device();
  157. if (in_dev == NULL) {
  158. pr_err("%s: input_allocate_device() failed\n", __func__);
  159. } else {
  160. /* Common part configuration */
  161. in_dev->name = name;
  162. in_dev->phys = NULL;
  163. in_dev->id.bustype = BUS_HOST;
  164. in_dev->id.vendor = 0x0001;
  165. in_dev->id.product = 0x0001;
  166. in_dev->id.version = USF_VERSION_ID;
  167. }
  168. return in_dev;
  169. }
  170. static int prepare_tsc_input_device(uint16_t ind,
  171. struct usf_type *usf_info,
  172. struct us_input_info_type *input_info,
  173. const char *name)
  174. {
  175. int i = 0;
  176. int num_buttons = min(ARRAY_SIZE(s_button_map),
  177. sizeof(input_info->req_buttons_bitmap) *
  178. BITS_IN_BYTE);
  179. uint16_t max_buttons_bitmap = ((1 << ARRAY_SIZE(s_button_map)) - 1);
  180. struct input_dev *in_dev = allocate_dev(ind, name);
  181. if (in_dev == NULL)
  182. return -ENOMEM;
  183. if (input_info->req_buttons_bitmap > max_buttons_bitmap) {
  184. pr_err("%s: Requested buttons[%d] exceeds max buttons available[%d]\n",
  185. __func__,
  186. input_info->req_buttons_bitmap,
  187. max_buttons_bitmap);
  188. input_free_device(in_dev);
  189. return -EINVAL;
  190. }
  191. usf_info->input_ifs[ind] = in_dev;
  192. usf_info->req_buttons_bitmap =
  193. input_info->req_buttons_bitmap;
  194. in_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  195. in_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  196. for (i = 0; i < num_buttons; i++)
  197. if (input_info->req_buttons_bitmap & (1 << i))
  198. in_dev->keybit[BIT_WORD(s_button_map[i])] |=
  199. BIT_MASK(s_button_map[i]);
  200. input_set_abs_params(in_dev, ABS_X,
  201. input_info->tsc_x_dim[MIN_IND],
  202. input_info->tsc_x_dim[MAX_IND],
  203. 0, 0);
  204. input_set_abs_params(in_dev, ABS_Y,
  205. input_info->tsc_y_dim[MIN_IND],
  206. input_info->tsc_y_dim[MAX_IND],
  207. 0, 0);
  208. input_set_abs_params(in_dev, ABS_DISTANCE,
  209. input_info->tsc_z_dim[MIN_IND],
  210. input_info->tsc_z_dim[MAX_IND],
  211. 0, 0);
  212. input_set_abs_params(in_dev, ABS_PRESSURE,
  213. input_info->tsc_pressure[MIN_IND],
  214. input_info->tsc_pressure[MAX_IND],
  215. 0, 0);
  216. input_set_abs_params(in_dev, ABS_TILT_X,
  217. input_info->tsc_x_tilt[MIN_IND],
  218. input_info->tsc_x_tilt[MAX_IND],
  219. 0, 0);
  220. input_set_abs_params(in_dev, ABS_TILT_Y,
  221. input_info->tsc_y_tilt[MIN_IND],
  222. input_info->tsc_y_tilt[MAX_IND],
  223. 0, 0);
  224. return 0;
  225. }
  226. static int prepare_mouse_input_device(uint16_t ind, struct usf_type *usf_info,
  227. struct us_input_info_type *input_info,
  228. const char *name)
  229. {
  230. struct input_dev *in_dev = allocate_dev(ind, name);
  231. if (in_dev == NULL)
  232. return -ENOMEM;
  233. usf_info->input_ifs[ind] = in_dev;
  234. in_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  235. in_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  236. BIT_MASK(BTN_RIGHT) |
  237. BIT_MASK(BTN_MIDDLE);
  238. in_dev->relbit[0] = BIT_MASK(REL_X) |
  239. BIT_MASK(REL_Y) |
  240. BIT_MASK(REL_Z);
  241. return 0;
  242. }
  243. static int prepare_keyboard_input_device(
  244. uint16_t ind,
  245. struct usf_type *usf_info,
  246. struct us_input_info_type *input_info,
  247. const char *name)
  248. {
  249. struct input_dev *in_dev = allocate_dev(ind, name);
  250. if (in_dev == NULL)
  251. return -ENOMEM;
  252. usf_info->input_ifs[ind] = in_dev;
  253. in_dev->evbit[0] |= BIT_MASK(EV_KEY);
  254. /* All keys are permitted */
  255. memset(in_dev->keybit, 0xff, sizeof(in_dev->keybit));
  256. return 0;
  257. }
  258. static void notify_tsc_event(struct usf_type *usf_info,
  259. uint16_t if_ind,
  260. struct usf_event_type *event)
  261. {
  262. int i = 0;
  263. int num_buttons = min(ARRAY_SIZE(s_button_map),
  264. sizeof(usf_info->req_buttons_bitmap) *
  265. BITS_IN_BYTE);
  266. struct input_dev *input_if = usf_info->input_ifs[if_ind];
  267. struct point_event_type *pe = &(event->event_data.point_event);
  268. input_report_abs(input_if, ABS_X, pe->coordinates[X_IND]);
  269. input_report_abs(input_if, ABS_Y, pe->coordinates[Y_IND]);
  270. input_report_abs(input_if, ABS_DISTANCE, pe->coordinates[Z_IND]);
  271. input_report_abs(input_if, ABS_TILT_X, pe->inclinations[X_IND]);
  272. input_report_abs(input_if, ABS_TILT_Y, pe->inclinations[Y_IND]);
  273. input_report_abs(input_if, ABS_PRESSURE, pe->pressure);
  274. input_report_key(input_if, BTN_TOUCH, !!(pe->pressure));
  275. for (i = 0; i < num_buttons; i++) {
  276. uint16_t mask = (1 << i),
  277. btn_state = !!(pe->buttons_state_bitmap & mask);
  278. if (usf_info->req_buttons_bitmap & mask)
  279. input_report_key(input_if, s_button_map[i], btn_state);
  280. }
  281. input_sync(input_if);
  282. pr_debug("%s: TSC event: xyz[%d;%d;%d], incl[%d;%d], pressure[%d], buttons[%d]\n",
  283. __func__,
  284. pe->coordinates[X_IND],
  285. pe->coordinates[Y_IND],
  286. pe->coordinates[Z_IND],
  287. pe->inclinations[X_IND],
  288. pe->inclinations[Y_IND],
  289. pe->pressure,
  290. pe->buttons_state_bitmap);
  291. }
  292. static void notify_mouse_event(struct usf_type *usf_info,
  293. uint16_t if_ind,
  294. struct usf_event_type *event)
  295. {
  296. struct input_dev *input_if = usf_info->input_ifs[if_ind];
  297. struct mouse_event_type *me = &(event->event_data.mouse_event);
  298. input_report_rel(input_if, REL_X, me->rels[X_IND]);
  299. input_report_rel(input_if, REL_Y, me->rels[Y_IND]);
  300. input_report_rel(input_if, REL_Z, me->rels[Z_IND]);
  301. input_report_key(input_if, BTN_LEFT,
  302. me->buttons_states & USF_BUTTON_LEFT_MASK);
  303. input_report_key(input_if, BTN_MIDDLE,
  304. me->buttons_states & USF_BUTTON_MIDDLE_MASK);
  305. input_report_key(input_if, BTN_RIGHT,
  306. me->buttons_states & USF_BUTTON_RIGHT_MASK);
  307. input_sync(input_if);
  308. pr_debug("%s: mouse event: dx[%d], dy[%d], buttons_states[%d]\n",
  309. __func__, me->rels[X_IND],
  310. me->rels[Y_IND], me->buttons_states);
  311. }
  312. static void notify_key_event(struct usf_type *usf_info,
  313. uint16_t if_ind,
  314. struct usf_event_type *event)
  315. {
  316. struct input_dev *input_if = usf_info->input_ifs[if_ind];
  317. struct key_event_type *ke = &(event->event_data.key_event);
  318. input_report_key(input_if, ke->key, ke->key_state);
  319. input_sync(input_if);
  320. pr_debug("%s: key event: key[%d], state[%d]\n",
  321. __func__,
  322. ke->key,
  323. ke->key_state);
  324. }
  325. static struct usf_input_dev_type s_usf_input_devs[] = {
  326. {USF_TSC_EVENT, "usf_tsc",
  327. prepare_tsc_input_device, notify_tsc_event},
  328. {USF_TSC_PTR_EVENT, "usf_tsc_ptr",
  329. prepare_tsc_input_device, notify_tsc_event},
  330. {USF_MOUSE_EVENT, "usf_mouse",
  331. prepare_mouse_input_device, notify_mouse_event},
  332. {USF_KEYBOARD_EVENT, "usf_kb",
  333. prepare_keyboard_input_device, notify_key_event},
  334. {USF_TSC_EXT_EVENT, "usf_tsc_ext",
  335. prepare_tsc_input_device, notify_tsc_event},
  336. };
  337. static void usf_rx_cb(uint32_t opcode, uint32_t token,
  338. uint32_t *payload, void *priv)
  339. {
  340. struct usf_xx_type *usf_xx = (struct usf_xx_type *) priv;
  341. if (usf_xx == NULL) {
  342. pr_err("%s: the private data is NULL\n", __func__);
  343. return;
  344. }
  345. switch (opcode) {
  346. case Q6USM_EVENT_WRITE_DONE:
  347. wake_up(&usf_xx->wait);
  348. break;
  349. case RESET_EVENTS:
  350. pr_err("%s: received RESET_EVENTS\n", __func__);
  351. usf_xx->usf_state = USF_ADSP_RESTART_STATE;
  352. wake_up(&usf_xx->wait);
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. static void usf_tx_cb(uint32_t opcode, uint32_t token,
  359. uint32_t *payload, void *priv)
  360. {
  361. struct usf_xx_type *usf_xx = (struct usf_xx_type *) priv;
  362. if (usf_xx == NULL) {
  363. pr_err("%s: the private data is NULL\n", __func__);
  364. return;
  365. }
  366. switch (opcode) {
  367. case Q6USM_EVENT_READ_DONE:
  368. pr_debug("%s: acquiring %d msec wake lock\n", __func__,
  369. STAY_AWAKE_AFTER_READ_MSECS);
  370. __pm_wakeup_event(&usf_wakeup_source,
  371. STAY_AWAKE_AFTER_READ_MSECS);
  372. if (token == USM_WRONG_TOKEN)
  373. usf_xx->usf_state = USF_ERROR_STATE;
  374. usf_xx->new_region = token;
  375. wake_up(&usf_xx->wait);
  376. break;
  377. case Q6USM_EVENT_SIGNAL_DETECT_RESULT:
  378. usf_xx->us_detect_type = (payload[APR_US_DETECT_RESULT_IND]) ?
  379. USF_US_DETECT_YES :
  380. USF_US_DETECT_NO;
  381. wake_up(&usf_xx->wait);
  382. break;
  383. case APR_BASIC_RSP_RESULT:
  384. if (payload[APR_RESULT_IND]) {
  385. usf_xx->usf_state = USF_ERROR_STATE;
  386. usf_xx->new_region = USM_WRONG_TOKEN;
  387. wake_up(&usf_xx->wait);
  388. }
  389. break;
  390. case RESET_EVENTS:
  391. pr_err("%s: received RESET_EVENTS\n", __func__);
  392. usf_xx->usf_state = USF_ADSP_RESTART_STATE;
  393. wake_up(&usf_xx->wait);
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. static void release_xx(struct usf_xx_type *usf_xx)
  400. {
  401. if (usf_xx != NULL) {
  402. if (usf_xx->usc) {
  403. q6usm_us_client_free(usf_xx->usc);
  404. usf_xx->usc = NULL;
  405. }
  406. if (usf_xx->encdec_cfg.params != NULL) {
  407. kfree(usf_xx->encdec_cfg.params);
  408. usf_xx->encdec_cfg.params = NULL;
  409. }
  410. }
  411. }
  412. static void usf_disable(struct usf_xx_type *usf_xx)
  413. {
  414. if (usf_xx != NULL) {
  415. if ((usf_xx->usf_state != USF_IDLE_STATE) &&
  416. (usf_xx->usf_state != USF_OPENED_STATE)) {
  417. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  418. usf_xx->usf_state = USF_OPENED_STATE;
  419. wake_up(&usf_xx->wait);
  420. }
  421. release_xx(usf_xx);
  422. }
  423. }
  424. static int config_xx(struct usf_xx_type *usf_xx, struct us_xx_info_type *config)
  425. {
  426. int rc = 0;
  427. uint16_t data_map_size = 0;
  428. uint16_t min_map_size = 0;
  429. if ((usf_xx == NULL) ||
  430. (config == NULL))
  431. return -EINVAL;
  432. if ((config->buf_size == 0) ||
  433. (config->buf_size > USF_MAX_BUF_SIZE) ||
  434. (config->buf_num == 0) ||
  435. (config->buf_num > USF_MAX_BUF_NUM)) {
  436. pr_err("%s: wrong params: buf_size=%d; buf_num=%d\n",
  437. __func__, config->buf_size, config->buf_num);
  438. return -EINVAL;
  439. }
  440. data_map_size = sizeof(usf_xx->encdec_cfg.cfg_common.data_map);
  441. min_map_size = min(data_map_size, config->port_cnt);
  442. if (config->client_name != NULL) {
  443. if (strncpy_from_user(usf_xx->client_name,
  444. (char __user *)config->client_name,
  445. sizeof(usf_xx->client_name) - 1) < 0) {
  446. pr_err("%s: get client name failed\n", __func__);
  447. return -EINVAL;
  448. }
  449. }
  450. pr_debug("%s: name=%s; buf_size:%d; dev_id:0x%x; sample_rate:%d\n",
  451. __func__, usf_xx->client_name, config->buf_size,
  452. config->dev_id, config->sample_rate);
  453. pr_debug("%s: buf_num:%d; format:%d; port_cnt:%d; data_size=%d\n",
  454. __func__, config->buf_num, config->stream_format,
  455. config->port_cnt, config->params_data_size);
  456. pr_debug("%s: id[0]=%d, id[1]=%d, id[2]=%d, id[3]=%d, id[4]=%d,\n",
  457. __func__,
  458. config->port_id[0],
  459. config->port_id[1],
  460. config->port_id[2],
  461. config->port_id[3],
  462. config->port_id[4]);
  463. pr_debug("id[5]=%d, id[6]=%d, id[7]=%d\n",
  464. config->port_id[5],
  465. config->port_id[6],
  466. config->port_id[7]);
  467. /* q6usm allocation & configuration */
  468. usf_xx->buffer_size = config->buf_size;
  469. usf_xx->buffer_count = config->buf_num;
  470. usf_xx->encdec_cfg.cfg_common.bits_per_sample =
  471. config->bits_per_sample;
  472. usf_xx->encdec_cfg.cfg_common.sample_rate = config->sample_rate;
  473. /* AFE port e.g. AFE_PORT_ID_SLIMBUS_MULTI_CHAN_1_RX */
  474. usf_xx->encdec_cfg.cfg_common.dev_id = config->dev_id;
  475. usf_xx->encdec_cfg.cfg_common.ch_cfg = config->port_cnt;
  476. memcpy((void *)&usf_xx->encdec_cfg.cfg_common.data_map,
  477. (void *)config->port_id,
  478. min_map_size);
  479. usf_xx->encdec_cfg.format_id = config->stream_format;
  480. usf_xx->encdec_cfg.params_size = config->params_data_size;
  481. usf_xx->user_upd_info_na = 1; /* it's used in US_GET_TX_UPDATE */
  482. if (config->params_data_size > 0) { /* transparent data copy */
  483. usf_xx->encdec_cfg.params = kzalloc(config->params_data_size,
  484. GFP_KERNEL);
  485. /* False memory leak here - pointer in packed struct
  486. * is undetected by kmemleak tool
  487. */
  488. kmemleak_ignore(usf_xx->encdec_cfg.params);
  489. if (usf_xx->encdec_cfg.params == NULL) {
  490. pr_err("%s: params memory alloc[%d] failure\n",
  491. __func__,
  492. config->params_data_size);
  493. return -ENOMEM;
  494. }
  495. rc = copy_from_user(usf_xx->encdec_cfg.params,
  496. (uint8_t __user *)config->params_data,
  497. config->params_data_size);
  498. if (rc) {
  499. pr_err("%s: transparent data copy failure\n",
  500. __func__);
  501. kfree(usf_xx->encdec_cfg.params);
  502. usf_xx->encdec_cfg.params = NULL;
  503. return -EFAULT;
  504. }
  505. pr_debug("%s: params_size[%d]; params[%d,%d,%d,%d, %d]\n",
  506. __func__,
  507. config->params_data_size,
  508. usf_xx->encdec_cfg.params[0],
  509. usf_xx->encdec_cfg.params[1],
  510. usf_xx->encdec_cfg.params[2],
  511. usf_xx->encdec_cfg.params[3],
  512. usf_xx->encdec_cfg.params[4]
  513. );
  514. }
  515. usf_xx->usc = q6usm_us_client_alloc(usf_xx->cb, (void *)usf_xx);
  516. if (!usf_xx->usc) {
  517. pr_err("%s: Could not allocate q6usm client\n", __func__);
  518. rc = -EFAULT;
  519. }
  520. return rc;
  521. }
  522. static bool usf_match(uint16_t event_type_ind, struct input_dev *dev)
  523. {
  524. bool rc = false;
  525. rc = (event_type_ind < MAX_EVENT_TYPE_NUM) &&
  526. ((dev->name == NULL) ||
  527. strcmp(dev->name, USF_NAME_PREFIX));
  528. pr_debug("%s: name=[%s]; rc=%d\n",
  529. __func__, dev->name, rc);
  530. return rc;
  531. }
  532. static bool usf_register_conflicting_events(uint16_t event_types)
  533. {
  534. bool rc = true;
  535. uint16_t ind = 0;
  536. uint16_t mask = 1;
  537. for (ind = 0; ind < MAX_EVENT_TYPE_NUM; ++ind) {
  538. if (event_types & mask) {
  539. rc = usfcdev_register(ind, usf_match);
  540. if (!rc)
  541. break;
  542. }
  543. mask = mask << 1;
  544. }
  545. return rc;
  546. }
  547. static void usf_unregister_conflicting_events(uint16_t event_types)
  548. {
  549. uint16_t ind = 0;
  550. uint16_t mask = 1;
  551. for (ind = 0; ind < MAX_EVENT_TYPE_NUM; ++ind) {
  552. if (event_types & mask)
  553. usfcdev_unregister(ind);
  554. mask = mask << 1;
  555. }
  556. }
  557. static void usf_set_event_filters(struct usf_type *usf, uint16_t event_filters)
  558. {
  559. uint16_t ind = 0;
  560. uint16_t mask = 1;
  561. if (usf->conflicting_event_filters != event_filters) {
  562. for (ind = 0; ind < MAX_EVENT_TYPE_NUM; ++ind) {
  563. if (usf->conflicting_event_types & mask)
  564. usfcdev_set_filter(ind, event_filters&mask);
  565. mask = mask << 1;
  566. }
  567. usf->conflicting_event_filters = event_filters;
  568. }
  569. }
  570. static int register_input_device(struct usf_type *usf_info,
  571. struct us_input_info_type *input_info)
  572. {
  573. int rc = 0;
  574. bool ret = true;
  575. uint16_t ind = 0;
  576. if ((usf_info == NULL) ||
  577. (input_info == NULL) ||
  578. !(input_info->event_types & USF_ALL_EVENTS)) {
  579. pr_err("%s: wrong input parameter(s)\n", __func__);
  580. return -EINVAL;
  581. }
  582. for (ind = 0; ind < USF_MAX_EVENT_IND; ++ind) {
  583. if (usf_info->input_ifs[ind] != NULL) {
  584. pr_err("%s: input_if[%d] is already allocated\n",
  585. __func__, ind);
  586. return -EFAULT;
  587. }
  588. if ((input_info->event_types &
  589. s_usf_input_devs[ind].event_type) &&
  590. s_usf_input_devs[ind].prepare_dev) {
  591. rc = (*s_usf_input_devs[ind].prepare_dev)(
  592. ind,
  593. usf_info,
  594. input_info,
  595. s_usf_input_devs[ind].input_dev_name);
  596. if (rc)
  597. return rc;
  598. rc = input_register_device(usf_info->input_ifs[ind]);
  599. if (rc) {
  600. pr_err("%s: input_reg_dev() failed; rc=%d\n",
  601. __func__, rc);
  602. input_free_device(usf_info->input_ifs[ind]);
  603. usf_info->input_ifs[ind] = NULL;
  604. } else {
  605. usf_info->event_types |=
  606. s_usf_input_devs[ind].event_type;
  607. pr_debug("%s: input device[%s] was registered\n",
  608. __func__,
  609. s_usf_input_devs[ind].input_dev_name);
  610. }
  611. } /* supported event */
  612. } /* event types loop */
  613. ret = usf_register_conflicting_events(
  614. input_info->conflicting_event_types);
  615. if (ret)
  616. usf_info->conflicting_event_types =
  617. input_info->conflicting_event_types;
  618. return 0;
  619. }
  620. static void handle_input_event(struct usf_type *usf_info,
  621. uint16_t event_counter,
  622. struct usf_event_type __user *event)
  623. {
  624. uint16_t ind = 0;
  625. uint16_t events_num = 0;
  626. struct usf_event_type usf_events[USF_EVENTS_PORTION_SIZE];
  627. int rc = 0;
  628. if ((usf_info == NULL) ||
  629. (event == NULL) || (!event_counter)) {
  630. return;
  631. }
  632. while (event_counter > 0) {
  633. if (event_counter > USF_EVENTS_PORTION_SIZE) {
  634. events_num = USF_EVENTS_PORTION_SIZE;
  635. event_counter -= USF_EVENTS_PORTION_SIZE;
  636. } else {
  637. events_num = event_counter;
  638. event_counter = 0;
  639. }
  640. rc = copy_from_user(usf_events,
  641. (struct usf_event_type __user *)event,
  642. events_num * sizeof(struct usf_event_type));
  643. if (rc) {
  644. pr_err("%s: copy upd_rx_info from user; rc=%d\n",
  645. __func__, rc);
  646. return;
  647. }
  648. for (ind = 0; ind < events_num; ++ind) {
  649. struct usf_event_type *p_event = &usf_events[ind];
  650. uint16_t if_ind = p_event->event_type_ind;
  651. if ((if_ind >= USF_MAX_EVENT_IND) ||
  652. (usf_info->input_ifs[if_ind] == NULL))
  653. continue; /* event isn't supported */
  654. if (s_usf_input_devs[if_ind].notify_event)
  655. (*s_usf_input_devs[if_ind].notify_event)(
  656. usf_info,
  657. if_ind,
  658. p_event);
  659. } /* loop in the portion */
  660. } /* all events loop */
  661. }
  662. static int usf_start_tx(struct usf_xx_type *usf_xx)
  663. {
  664. int rc = q6usm_run(usf_xx->usc, 0, 0, 0);
  665. pr_debug("%s: tx: q6usm_run; rc=%d\n", __func__, rc);
  666. if (!rc) {
  667. if (usf_xx->buffer_count >= USM_MIN_BUF_CNT) {
  668. /* supply all buffers */
  669. rc = q6usm_read(usf_xx->usc,
  670. usf_xx->buffer_count);
  671. pr_debug("%s: q6usm_read[%d]\n",
  672. __func__, rc);
  673. if (rc)
  674. pr_err("%s: buf read failed",
  675. __func__);
  676. else
  677. usf_xx->usf_state =
  678. USF_WORK_STATE;
  679. } else
  680. usf_xx->usf_state =
  681. USF_WORK_STATE;
  682. }
  683. return rc;
  684. } /* usf_start_tx */
  685. static int usf_start_rx(struct usf_xx_type *usf_xx)
  686. {
  687. int rc = q6usm_run(usf_xx->usc, 0, 0, 0);
  688. pr_debug("%s: rx: q6usm_run; rc=%d\n",
  689. __func__, rc);
  690. if (!rc)
  691. usf_xx->usf_state = USF_WORK_STATE;
  692. return rc;
  693. } /* usf_start_rx */
  694. static int __usf_set_us_detection(struct usf_type *usf,
  695. struct us_detect_info_type *detect_info)
  696. {
  697. uint32_t timeout = 0;
  698. struct usm_session_cmd_detect_info *p_allocated_memory = NULL;
  699. struct usm_session_cmd_detect_info usm_detect_info;
  700. struct usm_session_cmd_detect_info *p_usm_detect_info =
  701. &usm_detect_info;
  702. uint32_t detect_info_size = sizeof(struct usm_session_cmd_detect_info);
  703. struct usf_xx_type *usf_xx = &usf->usf_tx;
  704. int rc = 0;
  705. if (detect_info->us_detector != US_DETECT_FW) {
  706. pr_err("%s: unsupported detector: %d\n",
  707. __func__, detect_info->us_detector);
  708. return -EINVAL;
  709. }
  710. if ((detect_info->params_data_size != 0) &&
  711. (detect_info->params_data != NULL)) {
  712. uint8_t *p_data = NULL;
  713. detect_info_size += detect_info->params_data_size;
  714. p_allocated_memory = kzalloc(detect_info_size, GFP_KERNEL);
  715. if (p_allocated_memory == NULL) {
  716. pr_err("%s: detect_info[%d] allocation failed\n",
  717. __func__, detect_info_size);
  718. return -ENOMEM;
  719. }
  720. p_usm_detect_info = p_allocated_memory;
  721. p_data = (uint8_t *)p_usm_detect_info +
  722. sizeof(struct usm_session_cmd_detect_info);
  723. rc = copy_from_user(p_data,
  724. (uint8_t __user *)(detect_info->params_data),
  725. detect_info->params_data_size);
  726. if (rc) {
  727. pr_err("%s: copy params from user; rc=%d\n",
  728. __func__, rc);
  729. kfree(p_allocated_memory);
  730. return -EFAULT;
  731. }
  732. p_usm_detect_info->algorithm_cfg_size =
  733. detect_info->params_data_size;
  734. } else
  735. usm_detect_info.algorithm_cfg_size = 0;
  736. p_usm_detect_info->detect_mode = detect_info->us_detect_mode;
  737. p_usm_detect_info->skip_interval = detect_info->skip_time;
  738. usf_xx->us_detect_type = USF_US_DETECT_UNDEF;
  739. rc = q6usm_set_us_detection(usf_xx->usc,
  740. p_usm_detect_info,
  741. detect_info_size);
  742. if (rc || (detect_info->detect_timeout == USF_NO_WAIT_TIMEOUT)) {
  743. kfree(p_allocated_memory);
  744. return rc;
  745. }
  746. /* Get US detection result */
  747. if (detect_info->detect_timeout == USF_INFINITIVE_TIMEOUT) {
  748. rc = wait_event_interruptible(usf_xx->wait,
  749. (usf_xx->us_detect_type !=
  750. USF_US_DETECT_UNDEF) ||
  751. (usf_xx->usf_state ==
  752. USF_ADSP_RESTART_STATE));
  753. } else {
  754. if (detect_info->detect_timeout == USF_DEFAULT_TIMEOUT)
  755. timeout = USF_TIMEOUT_JIFFIES;
  756. else
  757. timeout = detect_info->detect_timeout * HZ;
  758. }
  759. rc = wait_event_interruptible_timeout(usf_xx->wait,
  760. (usf_xx->us_detect_type !=
  761. USF_US_DETECT_UNDEF) ||
  762. (usf_xx->usf_state ==
  763. USF_ADSP_RESTART_STATE), timeout);
  764. /* In the case of aDSP restart, "no US" is assumed */
  765. if (usf_xx->usf_state == USF_ADSP_RESTART_STATE)
  766. rc = -EFAULT;
  767. /* In the case of timeout, "no US" is assumed */
  768. if (rc < 0)
  769. pr_err("%s: Getting US detection failed rc[%d]\n",
  770. __func__, rc);
  771. else {
  772. usf->usf_rx.us_detect_type = usf->usf_tx.us_detect_type;
  773. detect_info->is_us =
  774. (usf_xx->us_detect_type == USF_US_DETECT_YES);
  775. }
  776. kfree(p_allocated_memory);
  777. return rc;
  778. } /* __usf_set_us_detection */
  779. static int usf_set_us_detection(struct usf_type *usf, unsigned long arg)
  780. {
  781. struct us_detect_info_type detect_info;
  782. int rc = copy_from_user(&detect_info,
  783. (struct us_detect_info_type __user *) arg,
  784. sizeof(detect_info));
  785. if (rc) {
  786. pr_err("%s: copy detect_info from user; rc=%d\n",
  787. __func__, rc);
  788. return -EFAULT;
  789. }
  790. if (detect_info.params_data_size > USF_MAX_USER_BUF_SIZE) {
  791. pr_err("%s: user buffer size exceeds maximum\n",
  792. __func__);
  793. return -EFAULT;
  794. }
  795. rc = __usf_set_us_detection(usf, &detect_info);
  796. if (rc < 0) {
  797. pr_err("%s: set us detection failed; rc=%d\n",
  798. __func__, rc);
  799. return rc;
  800. }
  801. rc = copy_to_user((void __user *)arg,
  802. &detect_info,
  803. sizeof(detect_info));
  804. if (rc) {
  805. pr_err("%s: copy detect_info to user; rc=%d\n",
  806. __func__, rc);
  807. rc = -EFAULT;
  808. }
  809. return rc;
  810. } /* usf_set_us_detection */
  811. static int __usf_set_tx_info(struct usf_type *usf,
  812. struct us_tx_info_type *config_tx)
  813. {
  814. struct usf_xx_type *usf_xx = &usf->usf_tx;
  815. int rc = 0;
  816. usf_xx->new_region = USM_UNDEF_TOKEN;
  817. usf_xx->prev_region = USM_UNDEF_TOKEN;
  818. usf_xx->cb = usf_tx_cb;
  819. init_waitqueue_head(&usf_xx->wait);
  820. if (config_tx->us_xx_info.client_name != NULL) {
  821. int res = strncpy_from_user(
  822. usf_xx->client_name,
  823. (char __user *)(config_tx->us_xx_info.client_name),
  824. sizeof(usf_xx->client_name)-1);
  825. if (res < 0) {
  826. pr_err("%s: get client name failed\n",
  827. __func__);
  828. return -EINVAL;
  829. }
  830. }
  831. rc = config_xx(usf_xx, &(config_tx->us_xx_info));
  832. if (rc)
  833. return rc;
  834. rc = q6usm_open_read(usf_xx->usc,
  835. usf_xx->encdec_cfg.format_id);
  836. if (rc)
  837. return rc;
  838. rc = q6usm_us_client_buf_alloc(OUT, usf_xx->usc,
  839. usf_xx->buffer_size,
  840. usf_xx->buffer_count);
  841. if (rc) {
  842. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  843. return rc;
  844. }
  845. rc = q6usm_us_param_buf_alloc(OUT, usf_xx->usc,
  846. config_tx->us_xx_info.max_get_set_param_buf_size);
  847. if (rc) {
  848. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  849. return rc;
  850. }
  851. rc = q6usm_enc_cfg_blk(usf_xx->usc,
  852. &usf_xx->encdec_cfg);
  853. if (!rc &&
  854. (config_tx->input_info.event_types != USF_NO_EVENT)) {
  855. rc = register_input_device(usf,
  856. &(config_tx->input_info));
  857. }
  858. if (rc)
  859. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  860. else
  861. usf_xx->usf_state = USF_CONFIGURED_STATE;
  862. return rc;
  863. } /* __usf_set_tx_info */
  864. static int usf_set_tx_info(struct usf_type *usf, unsigned long arg)
  865. {
  866. struct us_tx_info_type config_tx;
  867. int rc = copy_from_user(&config_tx,
  868. (struct us_tx_info_type __user *) arg,
  869. sizeof(config_tx));
  870. if (rc) {
  871. pr_err("%s: copy config_tx from user; rc=%d\n",
  872. __func__, rc);
  873. return -EFAULT;
  874. }
  875. if (config_tx.us_xx_info.params_data_size > USF_MAX_USER_BUF_SIZE) {
  876. pr_err("%s: user buffer size exceeds maximum\n",
  877. __func__);
  878. return -EFAULT;
  879. }
  880. return __usf_set_tx_info(usf, &config_tx);
  881. } /* usf_set_tx_info */
  882. static int __usf_set_rx_info(struct usf_type *usf,
  883. struct us_rx_info_type *config_rx)
  884. {
  885. struct usf_xx_type *usf_xx = &usf->usf_rx;
  886. int rc = 0;
  887. usf_xx->new_region = USM_UNDEF_TOKEN;
  888. usf_xx->prev_region = USM_UNDEF_TOKEN;
  889. usf_xx->cb = usf_rx_cb;
  890. rc = config_xx(usf_xx, &(config_rx->us_xx_info));
  891. if (rc)
  892. return rc;
  893. rc = q6usm_open_write(usf_xx->usc,
  894. usf_xx->encdec_cfg.format_id);
  895. if (rc)
  896. return rc;
  897. rc = q6usm_us_client_buf_alloc(
  898. IN,
  899. usf_xx->usc,
  900. usf_xx->buffer_size,
  901. usf_xx->buffer_count);
  902. if (rc) {
  903. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  904. return rc;
  905. }
  906. rc = q6usm_us_param_buf_alloc(IN, usf_xx->usc,
  907. config_rx->us_xx_info.max_get_set_param_buf_size);
  908. if (rc) {
  909. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  910. return rc;
  911. }
  912. rc = q6usm_dec_cfg_blk(usf_xx->usc,
  913. &usf_xx->encdec_cfg);
  914. if (rc)
  915. (void)q6usm_cmd(usf_xx->usc, CMD_CLOSE);
  916. else {
  917. init_waitqueue_head(&usf_xx->wait);
  918. usf_xx->usf_state = USF_CONFIGURED_STATE;
  919. }
  920. return rc;
  921. } /* __usf_set_rx_info */
  922. static int usf_set_rx_info(struct usf_type *usf, unsigned long arg)
  923. {
  924. struct us_rx_info_type config_rx;
  925. int rc = copy_from_user(&config_rx,
  926. (struct us_rx_info_type __user *) arg,
  927. sizeof(config_rx));
  928. if (rc) {
  929. pr_err("%s: copy config_rx from user; rc=%d\n",
  930. __func__, rc);
  931. return -EFAULT;
  932. }
  933. if (config_rx.us_xx_info.params_data_size > USF_MAX_USER_BUF_SIZE) {
  934. pr_err("%s: user buffer size exceeds maximum\n",
  935. __func__);
  936. return -EFAULT;
  937. }
  938. return __usf_set_rx_info(usf, &config_rx);
  939. } /* usf_set_rx_info */
  940. static int __usf_get_tx_update(struct usf_type *usf,
  941. struct us_tx_update_info_type *upd_tx_info)
  942. {
  943. unsigned long prev_jiffies = 0;
  944. uint32_t timeout = 0;
  945. struct usf_xx_type *usf_xx = &usf->usf_tx;
  946. int rc = 0;
  947. if (!usf_xx->user_upd_info_na) {
  948. usf_set_event_filters(usf, upd_tx_info->event_filters);
  949. handle_input_event(usf,
  950. upd_tx_info->event_counter,
  951. upd_tx_info->event);
  952. /* Release available regions */
  953. rc = q6usm_read(usf_xx->usc,
  954. upd_tx_info->free_region);
  955. if (rc)
  956. return rc;
  957. } else
  958. usf_xx->user_upd_info_na = 0;
  959. /* Get data ready regions */
  960. if (upd_tx_info->timeout == USF_INFINITIVE_TIMEOUT) {
  961. rc = wait_event_interruptible(usf_xx->wait,
  962. (usf_xx->prev_region !=
  963. usf_xx->new_region) ||
  964. (usf_xx->usf_state !=
  965. USF_WORK_STATE));
  966. } else {
  967. if (upd_tx_info->timeout == USF_NO_WAIT_TIMEOUT)
  968. rc = (usf_xx->prev_region != usf_xx->new_region);
  969. else {
  970. prev_jiffies = jiffies;
  971. if (upd_tx_info->timeout == USF_DEFAULT_TIMEOUT) {
  972. timeout = USF_TIMEOUT_JIFFIES;
  973. rc = wait_event_timeout(
  974. usf_xx->wait,
  975. (usf_xx->prev_region !=
  976. usf_xx->new_region) ||
  977. (usf_xx->usf_state !=
  978. USF_WORK_STATE),
  979. timeout);
  980. } else {
  981. timeout = upd_tx_info->timeout * HZ;
  982. rc = wait_event_interruptible_timeout(
  983. usf_xx->wait,
  984. (usf_xx->prev_region !=
  985. usf_xx->new_region) ||
  986. (usf_xx->usf_state !=
  987. USF_WORK_STATE),
  988. timeout);
  989. }
  990. }
  991. if (!rc) {
  992. pr_debug("%s: timeout. prev_j=%lu; j=%lu\n",
  993. __func__, prev_jiffies, jiffies);
  994. pr_debug("%s: timeout. prev=%d; new=%d\n",
  995. __func__, usf_xx->prev_region,
  996. usf_xx->new_region);
  997. pr_debug("%s: timeout. free_region=%d;\n",
  998. __func__, upd_tx_info->free_region);
  999. if (usf_xx->prev_region ==
  1000. usf_xx->new_region) {
  1001. pr_err("%s:read data: timeout\n",
  1002. __func__);
  1003. return -ETIME;
  1004. }
  1005. }
  1006. }
  1007. if ((usf_xx->usf_state != USF_WORK_STATE) ||
  1008. (rc == -ERESTARTSYS)) {
  1009. pr_err("%s: Get ready region failure; state[%d]; rc[%d]\n",
  1010. __func__, usf_xx->usf_state, rc);
  1011. return -EINTR;
  1012. }
  1013. upd_tx_info->ready_region = usf_xx->new_region;
  1014. usf_xx->prev_region = upd_tx_info->ready_region;
  1015. if (upd_tx_info->ready_region == USM_WRONG_TOKEN) {
  1016. pr_err("%s: TX path corrupted; prev=%d\n",
  1017. __func__, usf_xx->prev_region);
  1018. return -EIO;
  1019. }
  1020. return rc;
  1021. } /* __usf_get_tx_update */
  1022. static int usf_get_tx_update(struct usf_type *usf, unsigned long arg)
  1023. {
  1024. struct us_tx_update_info_type upd_tx_info;
  1025. int rc = copy_from_user(&upd_tx_info,
  1026. (struct us_tx_update_info_type __user *) arg,
  1027. sizeof(upd_tx_info));
  1028. if (rc < 0) {
  1029. pr_err("%s: copy upd_tx_info from user; rc=%d\n",
  1030. __func__, rc);
  1031. return -EFAULT;
  1032. }
  1033. rc = __usf_get_tx_update(usf, &upd_tx_info);
  1034. if (rc < 0) {
  1035. pr_err("%s: get tx update failed; rc=%d\n",
  1036. __func__, rc);
  1037. return rc;
  1038. }
  1039. rc = copy_to_user((void __user *)arg,
  1040. &upd_tx_info,
  1041. sizeof(upd_tx_info));
  1042. if (rc) {
  1043. pr_err("%s: copy upd_tx_info to user; rc=%d\n",
  1044. __func__, rc);
  1045. rc = -EFAULT;
  1046. }
  1047. return rc;
  1048. } /* usf_get_tx_update */
  1049. static int __usf_set_rx_update(struct usf_xx_type *usf_xx,
  1050. struct us_rx_update_info_type *upd_rx_info)
  1051. {
  1052. int rc = 0;
  1053. /* Send available data regions */
  1054. if (upd_rx_info->ready_region !=
  1055. usf_xx->buffer_count) {
  1056. rc = q6usm_write(
  1057. usf_xx->usc,
  1058. upd_rx_info->ready_region);
  1059. if (rc)
  1060. return rc;
  1061. }
  1062. /* Get free regions */
  1063. rc = wait_event_timeout(
  1064. usf_xx->wait,
  1065. !q6usm_is_write_buf_full(
  1066. usf_xx->usc,
  1067. &(upd_rx_info->free_region)) ||
  1068. (usf_xx->usf_state == USF_IDLE_STATE),
  1069. USF_TIMEOUT_JIFFIES);
  1070. if (!rc) {
  1071. rc = -ETIME;
  1072. pr_err("%s:timeout. wait for write buf not full\n",
  1073. __func__);
  1074. } else {
  1075. if (usf_xx->usf_state !=
  1076. USF_WORK_STATE) {
  1077. pr_err("%s: RX: state[%d]\n",
  1078. __func__,
  1079. usf_xx->usf_state);
  1080. rc = -EINTR;
  1081. }
  1082. }
  1083. return rc;
  1084. } /* __usf_set_rx_update */
  1085. static int usf_set_rx_update(struct usf_xx_type *usf_xx, unsigned long arg)
  1086. {
  1087. struct us_rx_update_info_type upd_rx_info;
  1088. int rc = copy_from_user(&upd_rx_info,
  1089. (struct us_rx_update_info_type __user *) arg,
  1090. sizeof(upd_rx_info));
  1091. if (rc) {
  1092. pr_err("%s: copy upd_rx_info from user; rc=%d\n",
  1093. __func__, rc);
  1094. return -EFAULT;
  1095. }
  1096. rc = __usf_set_rx_update(usf_xx, &upd_rx_info);
  1097. if (rc < 0) {
  1098. pr_err("%s: set rx update failed; rc=%d\n",
  1099. __func__, rc);
  1100. return rc;
  1101. }
  1102. rc = copy_to_user((void __user *)arg,
  1103. &upd_rx_info,
  1104. sizeof(upd_rx_info));
  1105. if (rc) {
  1106. pr_err("%s: copy rx_info to user; rc=%d\n",
  1107. __func__, rc);
  1108. rc = -EFAULT;
  1109. }
  1110. return rc;
  1111. } /* usf_set_rx_update */
  1112. static void usf_release_input(struct usf_type *usf)
  1113. {
  1114. uint16_t ind = 0;
  1115. usf_unregister_conflicting_events(
  1116. usf->conflicting_event_types);
  1117. usf->conflicting_event_types = 0;
  1118. for (ind = 0; ind < USF_MAX_EVENT_IND; ++ind) {
  1119. if (usf->input_ifs[ind] == NULL)
  1120. continue;
  1121. input_unregister_device(usf->input_ifs[ind]);
  1122. usf->input_ifs[ind] = NULL;
  1123. pr_debug("%s input_unregister_device[%s]\n",
  1124. __func__,
  1125. s_usf_input_devs[ind].input_dev_name);
  1126. }
  1127. } /* usf_release_input */
  1128. static int usf_stop_tx(struct usf_type *usf)
  1129. {
  1130. struct usf_xx_type *usf_xx = &usf->usf_tx;
  1131. usf_release_input(usf);
  1132. usf_disable(usf_xx);
  1133. return 0;
  1134. } /* usf_stop_tx */
  1135. static int __usf_get_version(struct us_version_info_type *version_info)
  1136. {
  1137. int rc = 0;
  1138. if (version_info->buf_size < sizeof(DRV_VERSION)) {
  1139. pr_err("%s: buf_size (%d) < version string size (%zu)\n",
  1140. __func__, version_info->buf_size, sizeof(DRV_VERSION));
  1141. return -EINVAL;
  1142. }
  1143. rc = copy_to_user((void __user *)(version_info->pbuf),
  1144. DRV_VERSION,
  1145. sizeof(DRV_VERSION));
  1146. if (rc) {
  1147. pr_err("%s: copy to version_info.pbuf; rc=%d\n",
  1148. __func__, rc);
  1149. rc = -EFAULT;
  1150. }
  1151. return rc;
  1152. } /* __usf_get_version */
  1153. static int usf_get_version(unsigned long arg)
  1154. {
  1155. struct us_version_info_type version_info;
  1156. int rc = copy_from_user(&version_info,
  1157. (struct us_version_info_type __user *) arg,
  1158. sizeof(version_info));
  1159. if (rc) {
  1160. pr_err("%s: copy version_info from user; rc=%d\n",
  1161. __func__, rc);
  1162. return -EFAULT;
  1163. }
  1164. rc = __usf_get_version(&version_info);
  1165. if (rc < 0) {
  1166. pr_err("%s: get version failed; rc=%d\n",
  1167. __func__, rc);
  1168. return rc;
  1169. }
  1170. rc = copy_to_user((void __user *)arg,
  1171. &version_info,
  1172. sizeof(version_info));
  1173. if (rc) {
  1174. pr_err("%s: copy version_info to user; rc=%d\n",
  1175. __func__, rc);
  1176. rc = -EFAULT;
  1177. }
  1178. return rc;
  1179. } /* usf_get_version */
  1180. static int __usf_set_stream_param(struct usf_xx_type *usf_xx,
  1181. struct us_stream_param_type *set_stream_param,
  1182. int dir)
  1183. {
  1184. struct us_client *usc = usf_xx->usc;
  1185. struct us_port_data *port;
  1186. int rc = 0;
  1187. if (usc == NULL) {
  1188. pr_err("%s: usc is null\n",
  1189. __func__);
  1190. return -EFAULT;
  1191. }
  1192. port = &usc->port[dir];
  1193. if (port == NULL) {
  1194. pr_err("%s: port is null\n",
  1195. __func__);
  1196. return -EFAULT;
  1197. }
  1198. if (port->param_buf == NULL) {
  1199. pr_err("%s: parameter buffer is null\n",
  1200. __func__);
  1201. return -EFAULT;
  1202. }
  1203. if (set_stream_param->buf_size > port->param_buf_size) {
  1204. pr_err("%s: buf_size (%d) > maximum buf size (%d)\n",
  1205. __func__, set_stream_param->buf_size,
  1206. port->param_buf_size);
  1207. return -EINVAL;
  1208. }
  1209. if (set_stream_param->buf_size == 0) {
  1210. pr_err("%s: buf_size is 0\n", __func__);
  1211. return -EINVAL;
  1212. }
  1213. rc = copy_from_user(port->param_buf,
  1214. (uint8_t __user *) set_stream_param->pbuf,
  1215. set_stream_param->buf_size);
  1216. if (rc) {
  1217. pr_err("%s: copy param buf from user; rc=%d\n",
  1218. __func__, rc);
  1219. return -EFAULT;
  1220. }
  1221. rc = q6usm_set_us_stream_param(dir, usc, set_stream_param->module_id,
  1222. set_stream_param->param_id,
  1223. set_stream_param->buf_size);
  1224. if (rc) {
  1225. pr_err("%s: q6usm_set_us_stream_param failed; rc=%d\n",
  1226. __func__, rc);
  1227. return -EFAULT;
  1228. }
  1229. return rc;
  1230. }
  1231. static int usf_set_stream_param(struct usf_xx_type *usf_xx,
  1232. unsigned long arg, int dir)
  1233. {
  1234. struct us_stream_param_type set_stream_param;
  1235. int rc = 0;
  1236. rc = copy_from_user(&set_stream_param,
  1237. (struct us_stream_param_type __user *) arg,
  1238. sizeof(set_stream_param));
  1239. if (rc) {
  1240. pr_err("%s: copy set_stream_param from user; rc=%d\n",
  1241. __func__, rc);
  1242. return -EFAULT;
  1243. }
  1244. return __usf_set_stream_param(usf_xx, &set_stream_param, dir);
  1245. } /* usf_set_stream_param */
  1246. static int __usf_get_stream_param(struct usf_xx_type *usf_xx,
  1247. struct us_stream_param_type *get_stream_param,
  1248. int dir)
  1249. {
  1250. struct us_client *usc = usf_xx->usc;
  1251. struct us_port_data *port;
  1252. int rc = 0;
  1253. if (usc == NULL) {
  1254. pr_err("%s: us_client is null\n",
  1255. __func__);
  1256. return -EFAULT;
  1257. }
  1258. port = &usc->port[dir];
  1259. if (port->param_buf == NULL) {
  1260. pr_err("%s: parameter buffer is null\n",
  1261. __func__);
  1262. return -EFAULT;
  1263. }
  1264. if (get_stream_param->buf_size > port->param_buf_size) {
  1265. pr_err("%s: buf_size (%d) > maximum buf size (%d)\n",
  1266. __func__, get_stream_param->buf_size,
  1267. port->param_buf_size);
  1268. return -EINVAL;
  1269. }
  1270. if (get_stream_param->buf_size == 0) {
  1271. pr_err("%s: buf_size is 0\n", __func__);
  1272. return -EINVAL;
  1273. }
  1274. rc = q6usm_get_us_stream_param(dir, usc, get_stream_param->module_id,
  1275. get_stream_param->param_id,
  1276. get_stream_param->buf_size);
  1277. if (rc) {
  1278. pr_err("%s: q6usm_get_us_stream_param failed; rc=%d\n",
  1279. __func__, rc);
  1280. return -EFAULT;
  1281. }
  1282. rc = copy_to_user((uint8_t __user *) get_stream_param->pbuf,
  1283. port->param_buf,
  1284. get_stream_param->buf_size);
  1285. if (rc) {
  1286. pr_err("%s: copy param buf to user; rc=%d\n",
  1287. __func__, rc);
  1288. return -EFAULT;
  1289. }
  1290. return rc;
  1291. }
  1292. static int usf_get_stream_param(struct usf_xx_type *usf_xx,
  1293. unsigned long arg, int dir)
  1294. {
  1295. struct us_stream_param_type get_stream_param;
  1296. int rc = 0;
  1297. rc = copy_from_user(&get_stream_param,
  1298. (struct us_stream_param_type __user *) arg,
  1299. sizeof(get_stream_param));
  1300. if (rc) {
  1301. pr_err("%s: copy get_stream_param from user; rc=%d\n",
  1302. __func__, rc);
  1303. return -EFAULT;
  1304. }
  1305. return __usf_get_stream_param(usf_xx, &get_stream_param, dir);
  1306. } /* usf_get_stream_param */
  1307. static long __usf_ioctl(struct usf_type *usf,
  1308. unsigned int cmd,
  1309. unsigned long arg)
  1310. {
  1311. int rc = 0;
  1312. struct usf_xx_type *usf_xx = NULL;
  1313. switch (cmd) {
  1314. case US_START_TX: {
  1315. usf_xx = &usf->usf_tx;
  1316. if (usf_xx->usf_state == USF_CONFIGURED_STATE)
  1317. rc = usf_start_tx(usf_xx);
  1318. else {
  1319. pr_err("%s: start_tx: wrong state[%d]\n",
  1320. __func__,
  1321. usf_xx->usf_state);
  1322. return -EBADFD;
  1323. }
  1324. break;
  1325. }
  1326. case US_START_RX: {
  1327. usf_xx = &usf->usf_rx;
  1328. if (usf_xx->usf_state == USF_CONFIGURED_STATE)
  1329. rc = usf_start_rx(usf_xx);
  1330. else {
  1331. pr_err("%s: start_rx: wrong state[%d]\n",
  1332. __func__,
  1333. usf_xx->usf_state);
  1334. return -EBADFD;
  1335. }
  1336. break;
  1337. }
  1338. case US_SET_TX_INFO: {
  1339. usf_xx = &usf->usf_tx;
  1340. if (usf_xx->usf_state == USF_OPENED_STATE)
  1341. rc = usf_set_tx_info(usf, arg);
  1342. else {
  1343. pr_err("%s: set_tx_info: wrong state[%d]\n",
  1344. __func__,
  1345. usf_xx->usf_state);
  1346. return -EBADFD;
  1347. }
  1348. break;
  1349. } /* US_SET_TX_INFO */
  1350. case US_SET_RX_INFO: {
  1351. usf_xx = &usf->usf_rx;
  1352. if (usf_xx->usf_state == USF_OPENED_STATE)
  1353. rc = usf_set_rx_info(usf, arg);
  1354. else {
  1355. pr_err("%s: set_rx_info: wrong state[%d]\n",
  1356. __func__,
  1357. usf_xx->usf_state);
  1358. return -EBADFD;
  1359. }
  1360. break;
  1361. } /* US_SET_RX_INFO */
  1362. case US_GET_TX_UPDATE: {
  1363. struct usf_xx_type *usf_xx = &usf->usf_tx;
  1364. if (usf_xx->usf_state == USF_WORK_STATE)
  1365. rc = usf_get_tx_update(usf, arg);
  1366. else {
  1367. pr_err("%s: get_tx_update: wrong state[%d]\n", __func__,
  1368. usf_xx->usf_state);
  1369. rc = -EBADFD;
  1370. }
  1371. break;
  1372. } /* US_GET_TX_UPDATE */
  1373. case US_SET_RX_UPDATE: {
  1374. struct usf_xx_type *usf_xx = &usf->usf_rx;
  1375. if (usf_xx->usf_state == USF_WORK_STATE)
  1376. rc = usf_set_rx_update(usf_xx, arg);
  1377. else {
  1378. pr_err("%s: set_rx_update: wrong state[%d]\n",
  1379. __func__,
  1380. usf_xx->usf_state);
  1381. rc = -EBADFD;
  1382. }
  1383. break;
  1384. } /* US_SET_RX_UPDATE */
  1385. case US_STOP_TX: {
  1386. usf_xx = &usf->usf_tx;
  1387. if ((usf_xx->usf_state == USF_WORK_STATE)
  1388. || (usf_xx->usf_state == USF_ADSP_RESTART_STATE))
  1389. rc = usf_stop_tx(usf);
  1390. else {
  1391. pr_err("%s: stop_tx: wrong state[%d]\n",
  1392. __func__,
  1393. usf_xx->usf_state);
  1394. return -EBADFD;
  1395. }
  1396. break;
  1397. } /* US_STOP_TX */
  1398. case US_STOP_RX: {
  1399. usf_xx = &usf->usf_rx;
  1400. if ((usf_xx->usf_state == USF_WORK_STATE)
  1401. || (usf_xx->usf_state == USF_ADSP_RESTART_STATE))
  1402. usf_disable(usf_xx);
  1403. else {
  1404. pr_err("%s: stop_rx: wrong state[%d]\n",
  1405. __func__,
  1406. usf_xx->usf_state);
  1407. return -EBADFD;
  1408. }
  1409. break;
  1410. } /* US_STOP_RX */
  1411. case US_SET_DETECTION: {
  1412. struct usf_xx_type *usf_xx = &usf->usf_tx;
  1413. if (usf_xx->usf_state == USF_WORK_STATE)
  1414. rc = usf_set_us_detection(usf, arg);
  1415. else {
  1416. pr_err("%s: set us detection: wrong state[%d]\n",
  1417. __func__,
  1418. usf_xx->usf_state);
  1419. rc = -EBADFD;
  1420. }
  1421. break;
  1422. } /* US_SET_DETECTION */
  1423. case US_GET_VERSION: {
  1424. rc = usf_get_version(arg);
  1425. break;
  1426. } /* US_GET_VERSION */
  1427. case US_SET_TX_STREAM_PARAM: {
  1428. rc = usf_set_stream_param(&usf->usf_tx, arg, OUT);
  1429. break;
  1430. } /* US_SET_TX_STREAM_PARAM */
  1431. case US_GET_TX_STREAM_PARAM: {
  1432. rc = usf_get_stream_param(&usf->usf_tx, arg, OUT);
  1433. break;
  1434. } /* US_GET_TX_STREAM_PARAM */
  1435. case US_SET_RX_STREAM_PARAM: {
  1436. rc = usf_set_stream_param(&usf->usf_rx, arg, IN);
  1437. break;
  1438. } /* US_SET_RX_STREAM_PARAM */
  1439. case US_GET_RX_STREAM_PARAM: {
  1440. rc = usf_get_stream_param(&usf->usf_rx, arg, IN);
  1441. break;
  1442. } /* US_GET_RX_STREAM_PARAM */
  1443. default:
  1444. pr_err("%s: unsupported IOCTL command [%d]\n",
  1445. __func__,
  1446. cmd);
  1447. rc = -ENOTTY;
  1448. break;
  1449. }
  1450. if (rc &&
  1451. ((cmd == US_SET_TX_INFO) ||
  1452. (cmd == US_SET_RX_INFO)))
  1453. release_xx(usf_xx);
  1454. return rc;
  1455. } /* __usf_ioctl */
  1456. static long usf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1457. {
  1458. struct usf_type *usf = file->private_data;
  1459. int rc = 0;
  1460. mutex_lock(&usf->mutex);
  1461. rc = __usf_ioctl(usf, cmd, arg);
  1462. mutex_unlock(&usf->mutex);
  1463. return rc;
  1464. } /* usf_ioctl */
  1465. #ifdef CONFIG_COMPAT
  1466. #define US_SET_TX_INFO32 _IOW(USF_IOCTL_MAGIC, 0, \
  1467. struct us_tx_info_type32)
  1468. #define US_GET_TX_UPDATE32 _IOWR(USF_IOCTL_MAGIC, 2, \
  1469. struct us_tx_update_info_type32)
  1470. #define US_SET_RX_INFO32 _IOW(USF_IOCTL_MAGIC, 3, \
  1471. struct us_rx_info_type32)
  1472. #define US_SET_RX_UPDATE32 _IOWR(USF_IOCTL_MAGIC, 4, \
  1473. struct us_rx_update_info_type32)
  1474. #define US_SET_DETECTION32 _IOWR(USF_IOCTL_MAGIC, 8, \
  1475. struct us_detect_info_type32)
  1476. #define US_GET_VERSION32 _IOWR(USF_IOCTL_MAGIC, 9, \
  1477. struct us_version_info_type32)
  1478. #define US_SET_TX_STREAM_PARAM32 _IOW(USF_IOCTL_MAGIC, 10, \
  1479. struct us_stream_param_type32)
  1480. #define US_GET_TX_STREAM_PARAM32 _IOWR(USF_IOCTL_MAGIC, 11, \
  1481. struct us_stream_param_type32)
  1482. #define US_SET_RX_STREAM_PARAM32 _IOW(USF_IOCTL_MAGIC, 12, \
  1483. struct us_stream_param_type32)
  1484. #define US_GET_RX_STREAM_PARAM32 _IOWR(USF_IOCTL_MAGIC, 13, \
  1485. struct us_stream_param_type32)
  1486. /* Info structure common for TX and RX */
  1487. struct us_xx_info_type32 {
  1488. /* Input: general info */
  1489. /* Name of the client - event calculator, ptr to char */
  1490. const compat_uptr_t client_name;
  1491. /* Selected device identification, accepted in the kernel's CAD */
  1492. uint32_t dev_id;
  1493. /* 0 - point_epos type; (e.g. 1 - gr_mmrd) */
  1494. uint32_t stream_format;
  1495. /* Required sample rate in Hz */
  1496. uint32_t sample_rate;
  1497. /* Size of a buffer (bytes) for US data transfer between the module and USF */
  1498. uint32_t buf_size;
  1499. /* Number of the buffers for the US data transfer */
  1500. uint16_t buf_num;
  1501. /* Number of the microphones (TX) or speakers(RX) */
  1502. uint16_t port_cnt;
  1503. /* Microphones(TX) or speakers(RX) indexes in their enumeration */
  1504. uint8_t port_id[USF_MAX_PORT_NUM];
  1505. /* Bits per sample 16 or 32 */
  1506. uint16_t bits_per_sample;
  1507. /* Input: Transparent info for encoder in the LPASS */
  1508. /* Parameters data size in bytes */
  1509. uint16_t params_data_size;
  1510. /* Pointer to the parameters, ptr to uint8_t */
  1511. compat_uptr_t params_data;
  1512. /* Max size of buffer for get and set parameter */
  1513. uint32_t max_get_set_param_buf_size;
  1514. };
  1515. struct us_tx_info_type32 {
  1516. /* Common info. This struct includes ptr and therefore the 32 version */
  1517. struct us_xx_info_type32 us_xx_info;
  1518. /* Info specific for TX. This struct doesn't include long or ptr
  1519. * and therefore no 32 version
  1520. */
  1521. struct us_input_info_type input_info;
  1522. };
  1523. struct us_tx_update_info_type32 {
  1524. /* Input general: */
  1525. /* Number of calculated events */
  1526. uint16_t event_counter;
  1527. /* Calculated events or NULL, ptr to struct usf_event_type */
  1528. compat_uptr_t event;
  1529. /* Pointer (read index) to the end of available region */
  1530. /* in the shared US data memory */
  1531. uint32_t free_region;
  1532. /* Time (sec) to wait for data or special values: */
  1533. /* USF_NO_WAIT_TIMEOUT, USF_INFINITIVE_TIMEOUT, USF_DEFAULT_TIMEOUT */
  1534. uint32_t timeout;
  1535. /* Events (from conflicting devs) to be disabled/enabled */
  1536. uint16_t event_filters;
  1537. /* Input transparent data: */
  1538. /* Parameters size */
  1539. uint16_t params_data_size;
  1540. /* Pointer to the parameters, ptr to uint8_t */
  1541. compat_uptr_t params_data;
  1542. /* Output parameters: */
  1543. /* Pointer (write index) to the end of ready US data region */
  1544. /* in the shared memory */
  1545. uint32_t ready_region;
  1546. };
  1547. struct us_rx_info_type32 {
  1548. /* Common info */
  1549. struct us_xx_info_type32 us_xx_info;
  1550. /* Info specific for RX*/
  1551. };
  1552. struct us_rx_update_info_type32 {
  1553. /* Input general: */
  1554. /* Pointer (write index) to the end of ready US data region */
  1555. /* in the shared memory */
  1556. uint32_t ready_region;
  1557. /* Input transparent data: */
  1558. /* Parameters size */
  1559. uint16_t params_data_size;
  1560. /* pPointer to the parameters, ptr to uint8_t */
  1561. compat_uptr_t params_data;
  1562. /* Output parameters: */
  1563. /* Pointer (read index) to the end of available region */
  1564. /* in the shared US data memory */
  1565. uint32_t free_region;
  1566. };
  1567. struct us_detect_info_type32 {
  1568. /* US detection place (HW|FW) */
  1569. /* NA in the Active and OFF states */
  1570. enum us_detect_place_enum us_detector;
  1571. /* US detection mode */
  1572. enum us_detect_mode_enum us_detect_mode;
  1573. /* US data dropped during this time (msec) */
  1574. uint32_t skip_time;
  1575. /* Transparent data size */
  1576. uint16_t params_data_size;
  1577. /* Pointer to the transparent data, ptr to uint8_t */
  1578. compat_uptr_t params_data;
  1579. /* Time (sec) to wait for US presence event */
  1580. uint32_t detect_timeout;
  1581. /* Out parameter: US presence */
  1582. bool is_us;
  1583. };
  1584. struct us_version_info_type32 {
  1585. /* Size of memory for the version string */
  1586. uint16_t buf_size;
  1587. /* Pointer to the memory for the version string, ptr to char */
  1588. compat_uptr_t pbuf;
  1589. };
  1590. struct us_stream_param_type32 {
  1591. /* Id of module */
  1592. uint32_t module_id;
  1593. /* Id of parameter */
  1594. uint32_t param_id;
  1595. /* Size of memory of the parameter buffer */
  1596. uint32_t buf_size;
  1597. /* Pointer to the memory of the parameter buffer */
  1598. compat_uptr_t pbuf;
  1599. };
  1600. static void usf_compat_xx_info_type(struct us_xx_info_type32 *us_xx_info32,
  1601. struct us_xx_info_type *us_xx_info)
  1602. {
  1603. int i = 0;
  1604. us_xx_info->client_name = compat_ptr(us_xx_info32->client_name);
  1605. us_xx_info->dev_id = us_xx_info32->dev_id;
  1606. us_xx_info->stream_format = us_xx_info32->stream_format;
  1607. us_xx_info->sample_rate = us_xx_info32->sample_rate;
  1608. us_xx_info->buf_size = us_xx_info32->buf_size;
  1609. us_xx_info->buf_num = us_xx_info32->buf_num;
  1610. us_xx_info->port_cnt = us_xx_info32->port_cnt;
  1611. for (i = 0; i < USF_MAX_PORT_NUM; i++)
  1612. us_xx_info->port_id[i] = us_xx_info32->port_id[i];
  1613. us_xx_info->bits_per_sample = us_xx_info32->bits_per_sample;
  1614. us_xx_info->params_data_size = us_xx_info32->params_data_size;
  1615. us_xx_info->params_data = compat_ptr(us_xx_info32->params_data);
  1616. us_xx_info->max_get_set_param_buf_size =
  1617. us_xx_info32->max_get_set_param_buf_size;
  1618. }
  1619. static int usf_set_tx_info32(struct usf_type *usf, unsigned long arg)
  1620. {
  1621. struct us_tx_info_type32 config_tx32;
  1622. struct us_tx_info_type config_tx;
  1623. int rc = copy_from_user(&config_tx32,
  1624. (struct us_tx_info_type32 __user *) arg,
  1625. sizeof(config_tx32));
  1626. if (rc) {
  1627. pr_err("%s: copy config_tx from user; rc=%d\n",
  1628. __func__, rc);
  1629. return -EFAULT;
  1630. }
  1631. memset(&config_tx, 0, sizeof(config_tx));
  1632. usf_compat_xx_info_type(&(config_tx32.us_xx_info),
  1633. &(config_tx.us_xx_info));
  1634. config_tx.input_info = config_tx32.input_info;
  1635. return __usf_set_tx_info(usf, &config_tx);
  1636. } /* usf_set_tx_info 32*/
  1637. static int usf_set_rx_info32(struct usf_type *usf, unsigned long arg)
  1638. {
  1639. struct us_rx_info_type32 config_rx32;
  1640. struct us_rx_info_type config_rx;
  1641. int rc = copy_from_user(&config_rx32,
  1642. (struct us_rx_info_type32 __user *) arg,
  1643. sizeof(config_rx32));
  1644. if (rc) {
  1645. pr_err("%s: copy config_rx from user; rc=%d\n",
  1646. __func__, rc);
  1647. return -EFAULT;
  1648. }
  1649. memset(&config_rx, 0, sizeof(config_rx));
  1650. usf_compat_xx_info_type(&(config_rx32.us_xx_info),
  1651. &(config_rx.us_xx_info));
  1652. return __usf_set_rx_info(usf, &config_rx);
  1653. } /* usf_set_rx_info32 */
  1654. static int usf_get_tx_update32(struct usf_type *usf, unsigned long arg)
  1655. {
  1656. struct us_tx_update_info_type32 upd_tx_info32;
  1657. struct us_tx_update_info_type upd_tx_info;
  1658. int rc = copy_from_user(&upd_tx_info32,
  1659. (struct us_tx_update_info_type32 __user *) arg,
  1660. sizeof(upd_tx_info32));
  1661. if (rc) {
  1662. pr_err("%s: copy upd_tx_info32 from user; rc=%d\n",
  1663. __func__, rc);
  1664. return -EFAULT;
  1665. }
  1666. memset(&upd_tx_info, 0, sizeof(upd_tx_info));
  1667. upd_tx_info.event_counter = upd_tx_info32.event_counter;
  1668. upd_tx_info.event = compat_ptr(upd_tx_info32.event);
  1669. upd_tx_info.free_region = upd_tx_info32.free_region;
  1670. upd_tx_info.timeout = upd_tx_info32.timeout;
  1671. upd_tx_info.event_filters = upd_tx_info32.event_filters;
  1672. upd_tx_info.params_data_size = upd_tx_info32.params_data_size;
  1673. upd_tx_info.params_data = compat_ptr(upd_tx_info32.params_data);
  1674. upd_tx_info.ready_region = upd_tx_info32.ready_region;
  1675. rc = __usf_get_tx_update(usf, &upd_tx_info);
  1676. if (rc < 0) {
  1677. pr_err("%s: get tx update failed; rc=%d\n",
  1678. __func__, rc);
  1679. return rc;
  1680. }
  1681. /* Update only the fields that were changed */
  1682. upd_tx_info32.ready_region = upd_tx_info.ready_region;
  1683. rc = copy_to_user((void __user *)arg, &upd_tx_info32,
  1684. sizeof(upd_tx_info32));
  1685. if (rc) {
  1686. pr_err("%s: copy upd_tx_info32 to user; rc=%d\n",
  1687. __func__, rc);
  1688. rc = -EFAULT;
  1689. }
  1690. return rc;
  1691. } /* usf_get_tx_update */
  1692. static int usf_set_rx_update32(struct usf_xx_type *usf_xx, unsigned long arg)
  1693. {
  1694. struct us_rx_update_info_type32 upd_rx_info32;
  1695. struct us_rx_update_info_type upd_rx_info;
  1696. int rc = copy_from_user(&upd_rx_info32,
  1697. (struct us_rx_update_info_type32 __user *) arg,
  1698. sizeof(upd_rx_info32));
  1699. if (rc) {
  1700. pr_err("%s: copy upd_rx_info32 from user; rc=%d\n",
  1701. __func__, rc);
  1702. return -EFAULT;
  1703. }
  1704. memset(&upd_rx_info, 0, sizeof(upd_rx_info));
  1705. upd_rx_info.ready_region = upd_rx_info32.ready_region;
  1706. upd_rx_info.params_data_size = upd_rx_info32.params_data_size;
  1707. upd_rx_info.params_data = compat_ptr(upd_rx_info32.params_data);
  1708. upd_rx_info.free_region = upd_rx_info32.free_region;
  1709. rc = __usf_set_rx_update(usf_xx, &upd_rx_info);
  1710. if (rc < 0) {
  1711. pr_err("%s: set rx update failed; rc=%d\n",
  1712. __func__, rc);
  1713. return rc;
  1714. }
  1715. /* Update only the fields that were changed */
  1716. upd_rx_info32.free_region = upd_rx_info.free_region;
  1717. rc = copy_to_user((void __user *)arg,
  1718. &upd_rx_info32,
  1719. sizeof(upd_rx_info32));
  1720. if (rc) {
  1721. pr_err("%s: copy rx_info32 to user; rc=%d\n",
  1722. __func__, rc);
  1723. rc = -EFAULT;
  1724. }
  1725. return rc;
  1726. } /* usf_set_rx_update32 */
  1727. static int usf_set_us_detection32(struct usf_type *usf, unsigned long arg)
  1728. {
  1729. struct us_detect_info_type32 detect_info32;
  1730. struct us_detect_info_type detect_info;
  1731. int rc = copy_from_user(&detect_info32,
  1732. (struct us_detect_info_type32 __user *) arg,
  1733. sizeof(detect_info32));
  1734. if (rc) {
  1735. pr_err("%s: copy detect_info32 from user; rc=%d\n",
  1736. __func__, rc);
  1737. return -EFAULT;
  1738. }
  1739. if (detect_info32.params_data_size > USF_MAX_USER_BUF_SIZE) {
  1740. pr_err("%s: user buffer size exceeds maximum\n",
  1741. __func__);
  1742. return -EFAULT;
  1743. }
  1744. memset(&detect_info, 0, sizeof(detect_info));
  1745. detect_info.us_detector = detect_info32.us_detector;
  1746. detect_info.us_detect_mode = detect_info32.us_detect_mode;
  1747. detect_info.skip_time = detect_info32.skip_time;
  1748. detect_info.params_data_size = detect_info32.params_data_size;
  1749. detect_info.params_data = compat_ptr(detect_info32.params_data);
  1750. detect_info.detect_timeout = detect_info32.detect_timeout;
  1751. detect_info.is_us = detect_info32.is_us;
  1752. rc = __usf_set_us_detection(usf, &detect_info);
  1753. if (rc < 0) {
  1754. pr_err("%s: set us detection failed; rc=%d\n",
  1755. __func__, rc);
  1756. return rc;
  1757. }
  1758. /* Update only the fields that were changed */
  1759. detect_info32.is_us = detect_info.is_us;
  1760. rc = copy_to_user((void __user *)arg,
  1761. &detect_info32,
  1762. sizeof(detect_info32));
  1763. if (rc) {
  1764. pr_err("%s: copy detect_info32 to user; rc=%d\n",
  1765. __func__, rc);
  1766. rc = -EFAULT;
  1767. }
  1768. return rc;
  1769. } /* usf_set_us_detection32 */
  1770. static int usf_get_version32(unsigned long arg)
  1771. {
  1772. struct us_version_info_type32 version_info32;
  1773. struct us_version_info_type version_info;
  1774. int rc = copy_from_user(&version_info32,
  1775. (struct us_version_info_type32 __user *) arg,
  1776. sizeof(version_info32));
  1777. if (rc) {
  1778. pr_err("%s: copy version_info32 from user; rc=%d\n",
  1779. __func__, rc);
  1780. return -EFAULT;
  1781. }
  1782. memset(&version_info, 0, sizeof(version_info));
  1783. version_info.buf_size = version_info32.buf_size;
  1784. version_info.pbuf = compat_ptr(version_info32.pbuf);
  1785. rc = __usf_get_version(&version_info);
  1786. if (rc < 0) {
  1787. pr_err("%s: get version failed; rc=%d\n",
  1788. __func__, rc);
  1789. return rc;
  1790. }
  1791. /* None of the fields were changed */
  1792. rc = copy_to_user((void __user *)arg,
  1793. &version_info32,
  1794. sizeof(version_info32));
  1795. if (rc) {
  1796. pr_err("%s: copy version_info32 to user; rc=%d\n",
  1797. __func__, rc);
  1798. rc = -EFAULT;
  1799. }
  1800. return rc;
  1801. } /* usf_get_version32 */
  1802. static int usf_set_stream_param32(struct usf_xx_type *usf_xx,
  1803. unsigned long arg, int dir)
  1804. {
  1805. struct us_stream_param_type32 set_stream_param32;
  1806. struct us_stream_param_type set_stream_param;
  1807. int rc = 0;
  1808. rc = copy_from_user(&set_stream_param32,
  1809. (struct us_stream_param_type32 __user *) arg,
  1810. sizeof(set_stream_param32));
  1811. if (rc) {
  1812. pr_err("%s: copy set_stream_param from user; rc=%d\n",
  1813. __func__, rc);
  1814. return -EFAULT;
  1815. }
  1816. memset(&set_stream_param, 0, sizeof(set_stream_param));
  1817. set_stream_param.module_id = set_stream_param32.module_id;
  1818. set_stream_param.param_id = set_stream_param32.param_id;
  1819. set_stream_param.buf_size = set_stream_param32.buf_size;
  1820. set_stream_param.pbuf = compat_ptr(set_stream_param32.pbuf);
  1821. return __usf_set_stream_param(usf_xx, &set_stream_param, dir);
  1822. } /* usf_set_stream_param32 */
  1823. static int usf_get_stream_param32(struct usf_xx_type *usf_xx,
  1824. unsigned long arg, int dir)
  1825. {
  1826. struct us_stream_param_type32 get_stream_param32;
  1827. struct us_stream_param_type get_stream_param;
  1828. int rc = 0;
  1829. rc = copy_from_user(&get_stream_param32,
  1830. (struct us_stream_param_type32 __user *) arg,
  1831. sizeof(get_stream_param32));
  1832. if (rc) {
  1833. pr_err("%s: copy get_stream_param from user; rc=%d\n",
  1834. __func__, rc);
  1835. return -EFAULT;
  1836. }
  1837. memset(&get_stream_param, 0, sizeof(get_stream_param));
  1838. get_stream_param.module_id = get_stream_param32.module_id;
  1839. get_stream_param.param_id = get_stream_param32.param_id;
  1840. get_stream_param.buf_size = get_stream_param32.buf_size;
  1841. get_stream_param.pbuf = compat_ptr(get_stream_param32.pbuf);
  1842. return __usf_get_stream_param(usf_xx, &get_stream_param, dir);
  1843. } /* usf_get_stream_param32 */
  1844. static long __usf_compat_ioctl(struct usf_type *usf,
  1845. unsigned int cmd,
  1846. unsigned long arg)
  1847. {
  1848. int rc = 0;
  1849. struct usf_xx_type *usf_xx = NULL;
  1850. switch (cmd) {
  1851. case US_START_TX:
  1852. case US_START_RX:
  1853. case US_STOP_TX:
  1854. case US_STOP_RX: {
  1855. return __usf_ioctl(usf, cmd, arg);
  1856. }
  1857. case US_SET_TX_INFO32: {
  1858. usf_xx = &usf->usf_tx;
  1859. if (usf_xx->usf_state == USF_OPENED_STATE)
  1860. rc = usf_set_tx_info32(usf, arg);
  1861. else {
  1862. pr_err("%s: set_tx_info32: wrong state[%d]\n",
  1863. __func__,
  1864. usf_xx->usf_state);
  1865. return -EBADFD;
  1866. }
  1867. break;
  1868. } /* US_SET_TX_INFO32 */
  1869. case US_SET_RX_INFO32: {
  1870. usf_xx = &usf->usf_rx;
  1871. if (usf_xx->usf_state == USF_OPENED_STATE)
  1872. rc = usf_set_rx_info32(usf, arg);
  1873. else {
  1874. pr_err("%s: set_rx_info32: wrong state[%d]\n",
  1875. __func__,
  1876. usf_xx->usf_state);
  1877. return -EBADFD;
  1878. }
  1879. break;
  1880. } /* US_SET_RX_INFO32 */
  1881. case US_GET_TX_UPDATE32: {
  1882. struct usf_xx_type *usf_xx = &usf->usf_tx;
  1883. if (usf_xx->usf_state == USF_WORK_STATE)
  1884. rc = usf_get_tx_update32(usf, arg);
  1885. else {
  1886. pr_err("%s: get_tx_update32: wrong state[%d]\n",
  1887. __func__,
  1888. usf_xx->usf_state);
  1889. rc = -EBADFD;
  1890. }
  1891. break;
  1892. } /* US_GET_TX_UPDATE32 */
  1893. case US_SET_RX_UPDATE32: {
  1894. struct usf_xx_type *usf_xx = &usf->usf_rx;
  1895. if (usf_xx->usf_state == USF_WORK_STATE)
  1896. rc = usf_set_rx_update32(usf_xx, arg);
  1897. else {
  1898. pr_err("%s: set_rx_update: wrong state[%d]\n",
  1899. __func__,
  1900. usf_xx->usf_state);
  1901. rc = -EBADFD;
  1902. }
  1903. break;
  1904. } /* US_SET_RX_UPDATE32 */
  1905. case US_SET_DETECTION32: {
  1906. struct usf_xx_type *usf_xx = &usf->usf_tx;
  1907. if (usf_xx->usf_state == USF_WORK_STATE)
  1908. rc = usf_set_us_detection32(usf, arg);
  1909. else {
  1910. pr_err("%s: set us detection: wrong state[%d]\n",
  1911. __func__,
  1912. usf_xx->usf_state);
  1913. rc = -EBADFD;
  1914. }
  1915. break;
  1916. } /* US_SET_DETECTION32 */
  1917. case US_GET_VERSION32: {
  1918. rc = usf_get_version32(arg);
  1919. break;
  1920. } /* US_GET_VERSION32 */
  1921. case US_SET_TX_STREAM_PARAM32: {
  1922. rc = usf_set_stream_param32(&usf->usf_tx, arg, OUT);
  1923. break;
  1924. } /* US_SET_TX_STREAM_PARAM32 */
  1925. case US_GET_TX_STREAM_PARAM32: {
  1926. rc = usf_get_stream_param32(&usf->usf_tx, arg, OUT);
  1927. break;
  1928. } /* US_GET_TX_STREAM_PARAM32 */
  1929. case US_SET_RX_STREAM_PARAM32: {
  1930. rc = usf_set_stream_param32(&usf->usf_rx, arg, IN);
  1931. break;
  1932. } /* US_SET_RX_STREAM_PARAM32 */
  1933. case US_GET_RX_STREAM_PARAM32: {
  1934. rc = usf_get_stream_param32(&usf->usf_rx, arg, IN);
  1935. break;
  1936. } /* US_GET_RX_STREAM_PARAM32 */
  1937. default:
  1938. pr_err("%s: unsupported IOCTL command [%d]\n",
  1939. __func__,
  1940. cmd);
  1941. rc = -ENOTTY;
  1942. break;
  1943. }
  1944. if (rc &&
  1945. ((cmd == US_SET_TX_INFO) ||
  1946. (cmd == US_SET_RX_INFO)))
  1947. release_xx(usf_xx);
  1948. return rc;
  1949. } /* __usf_compat_ioctl */
  1950. static long usf_compat_ioctl(struct file *file,
  1951. unsigned int cmd,
  1952. unsigned long arg)
  1953. {
  1954. struct usf_type *usf = file->private_data;
  1955. int rc = 0;
  1956. mutex_lock(&usf->mutex);
  1957. rc = __usf_compat_ioctl(usf, cmd, arg);
  1958. mutex_unlock(&usf->mutex);
  1959. return rc;
  1960. } /* usf_compat_ioctl */
  1961. #endif /* CONFIG_COMPAT */
  1962. static int usf_mmap(struct file *file, struct vm_area_struct *vms)
  1963. {
  1964. struct usf_type *usf = file->private_data;
  1965. int dir = OUT;
  1966. struct usf_xx_type *usf_xx = &usf->usf_tx;
  1967. int rc = 0;
  1968. mutex_lock(&usf->mutex);
  1969. if (vms->vm_flags & USF_VM_WRITE) { /* RX buf mapping */
  1970. dir = IN;
  1971. usf_xx = &usf->usf_rx;
  1972. }
  1973. rc = q6usm_get_virtual_address(dir, usf_xx->usc, vms);
  1974. mutex_unlock(&usf->mutex);
  1975. return rc;
  1976. }
  1977. static uint16_t add_opened_dev(int minor)
  1978. {
  1979. uint16_t ind = 0;
  1980. for (ind = 0; ind < MAX_DEVS_NUMBER; ++ind) {
  1981. if (minor == atomic_cmpxchg(&s_opened_devs[ind], 0, minor)) {
  1982. pr_err("%s: device %d is already opened\n",
  1983. __func__, minor);
  1984. return USF_UNDEF_DEV_ID;
  1985. } else {
  1986. pr_debug("%s: device %d is added; ind=%d\n",
  1987. __func__, minor, ind);
  1988. return ind;
  1989. }
  1990. }
  1991. pr_err("%s: there is no place for device %d\n",
  1992. __func__, minor);
  1993. return USF_UNDEF_DEV_ID;
  1994. }
  1995. static int usf_open(struct inode *inode, struct file *file)
  1996. {
  1997. struct usf_type *usf = NULL;
  1998. uint16_t dev_ind = 0;
  1999. int minor = MINOR(inode->i_rdev);
  2000. dev_ind = add_opened_dev(minor);
  2001. if (dev_ind == USF_UNDEF_DEV_ID)
  2002. return -EBUSY;
  2003. usf = kzalloc(sizeof(struct usf_type), GFP_KERNEL);
  2004. if (usf == NULL)
  2005. return -ENOMEM;
  2006. wakeup_source_init(&usf_wakeup_source, "usf");
  2007. file->private_data = usf;
  2008. usf->dev_ind = dev_ind;
  2009. usf->usf_tx.usf_state = USF_OPENED_STATE;
  2010. usf->usf_rx.usf_state = USF_OPENED_STATE;
  2011. usf->usf_tx.us_detect_type = USF_US_DETECT_UNDEF;
  2012. usf->usf_rx.us_detect_type = USF_US_DETECT_UNDEF;
  2013. mutex_init(&usf->mutex);
  2014. pr_debug("%s:usf in open\n", __func__);
  2015. return 0;
  2016. }
  2017. static int usf_release(struct inode *inode, struct file *file)
  2018. {
  2019. struct usf_type *usf = file->private_data;
  2020. pr_debug("%s: release entry\n", __func__);
  2021. mutex_lock(&usf->mutex);
  2022. usf_release_input(usf);
  2023. usf_disable(&usf->usf_tx);
  2024. usf_disable(&usf->usf_rx);
  2025. atomic_set(&s_opened_devs[usf->dev_ind], 0);
  2026. wakeup_source_trash(&usf_wakeup_source);
  2027. mutex_unlock(&usf->mutex);
  2028. mutex_destroy(&usf->mutex);
  2029. kfree(usf);
  2030. pr_debug("%s: release exit\n", __func__);
  2031. return 0;
  2032. }
  2033. extern long usf_compat_ioctl(struct file *file,
  2034. unsigned int cmd,
  2035. unsigned long arg);
  2036. static const struct file_operations usf_fops = {
  2037. .owner = THIS_MODULE,
  2038. .open = usf_open,
  2039. .release = usf_release,
  2040. .unlocked_ioctl = usf_ioctl,
  2041. #ifdef CONFIG_COMPAT
  2042. .compat_ioctl = usf_compat_ioctl,
  2043. #endif /* CONFIG_COMPAT */
  2044. .mmap = usf_mmap,
  2045. };
  2046. static struct miscdevice usf_misc[MAX_DEVS_NUMBER] = {
  2047. {
  2048. .minor = MISC_DYNAMIC_MINOR,
  2049. .name = "usf1",
  2050. .fops = &usf_fops,
  2051. },
  2052. };
  2053. static int __init usf_init(void)
  2054. {
  2055. int rc = 0;
  2056. uint16_t ind = 0;
  2057. pr_debug("%s: USF SW version %s.\n", __func__, DRV_VERSION);
  2058. pr_debug("%s: Max %d devs registration\n", __func__, MAX_DEVS_NUMBER);
  2059. for (ind = 0; ind < MAX_DEVS_NUMBER; ++ind) {
  2060. rc = misc_register(&usf_misc[ind]);
  2061. if (rc) {
  2062. pr_err("%s: misc_register() failed ind=%d; rc = %d\n",
  2063. __func__, ind, rc);
  2064. break;
  2065. }
  2066. }
  2067. if (!rc) q6usm_init();
  2068. return rc;
  2069. }
  2070. module_init(usf_init);
  2071. static void __exit usf_exit(void)
  2072. {
  2073. uint16_t ind = 0;
  2074. for (ind = 0; ind < MAX_DEVS_NUMBER; ++ind)
  2075. misc_deregister(&usf_misc[ind]);
  2076. }
  2077. module_exit(usf_exit);
  2078. MODULE_DESCRIPTION("Ultrasound framework driver");
  2079. MODULE_LICENSE("GPL v2");