usf.c 62 KB

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