core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * core.c - Implementation of core module of MOST Linux driver stack
  4. *
  5. * Copyright (C) 2013-2020 Microchip Technology Germany II GmbH & Co. KG
  6. */
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/list.h>
  13. #include <linux/poll.h>
  14. #include <linux/wait.h>
  15. #include <linux/kobject.h>
  16. #include <linux/mutex.h>
  17. #include <linux/completion.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/kthread.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/idr.h>
  22. #include <linux/most.h>
  23. #define MAX_CHANNELS 64
  24. #define STRING_SIZE 80
  25. static struct ida mdev_id;
  26. static int dummy_num_buffers;
  27. static struct list_head comp_list;
  28. struct pipe {
  29. struct most_component *comp;
  30. int refs;
  31. int num_buffers;
  32. };
  33. struct most_channel {
  34. struct device dev;
  35. struct completion cleanup;
  36. atomic_t mbo_ref;
  37. atomic_t mbo_nq_level;
  38. u16 channel_id;
  39. char name[STRING_SIZE];
  40. bool is_poisoned;
  41. struct mutex start_mutex; /* channel activation synchronization */
  42. struct mutex nq_mutex; /* nq thread synchronization */
  43. int is_starving;
  44. struct most_interface *iface;
  45. struct most_channel_config cfg;
  46. bool keep_mbo;
  47. bool enqueue_halt;
  48. struct list_head fifo;
  49. spinlock_t fifo_lock; /* fifo access synchronization */
  50. struct list_head halt_fifo;
  51. struct list_head list;
  52. struct pipe pipe0;
  53. struct pipe pipe1;
  54. struct list_head trash_fifo;
  55. struct task_struct *hdm_enqueue_task;
  56. wait_queue_head_t hdm_fifo_wq;
  57. };
  58. #define to_channel(d) container_of(d, struct most_channel, dev)
  59. struct interface_private {
  60. int dev_id;
  61. char name[STRING_SIZE];
  62. struct most_channel *channel[MAX_CHANNELS];
  63. struct list_head channel_list;
  64. };
  65. static const struct {
  66. int most_ch_data_type;
  67. const char *name;
  68. } ch_data_type[] = {
  69. { MOST_CH_CONTROL, "control" },
  70. { MOST_CH_ASYNC, "async" },
  71. { MOST_CH_SYNC, "sync" },
  72. { MOST_CH_ISOC, "isoc"},
  73. { MOST_CH_ISOC, "isoc_avp"},
  74. };
  75. /**
  76. * list_pop_mbo - retrieves the first MBO of the list and removes it
  77. * @ptr: the list head to grab the MBO from.
  78. */
  79. #define list_pop_mbo(ptr) \
  80. ({ \
  81. struct mbo *_mbo = list_first_entry(ptr, struct mbo, list); \
  82. list_del(&_mbo->list); \
  83. _mbo; \
  84. })
  85. /**
  86. * most_free_mbo_coherent - free an MBO and its coherent buffer
  87. * @mbo: most buffer
  88. */
  89. static void most_free_mbo_coherent(struct mbo *mbo)
  90. {
  91. struct most_channel *c = mbo->context;
  92. u16 const coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
  93. if (c->iface->dma_free)
  94. c->iface->dma_free(mbo, coherent_buf_size);
  95. else
  96. kfree(mbo->virt_address);
  97. kfree(mbo);
  98. if (atomic_sub_and_test(1, &c->mbo_ref))
  99. complete(&c->cleanup);
  100. }
  101. /**
  102. * flush_channel_fifos - clear the channel fifos
  103. * @c: pointer to channel object
  104. */
  105. static void flush_channel_fifos(struct most_channel *c)
  106. {
  107. unsigned long flags, hf_flags;
  108. struct mbo *mbo, *tmp;
  109. if (list_empty(&c->fifo) && list_empty(&c->halt_fifo))
  110. return;
  111. spin_lock_irqsave(&c->fifo_lock, flags);
  112. list_for_each_entry_safe(mbo, tmp, &c->fifo, list) {
  113. list_del(&mbo->list);
  114. spin_unlock_irqrestore(&c->fifo_lock, flags);
  115. most_free_mbo_coherent(mbo);
  116. spin_lock_irqsave(&c->fifo_lock, flags);
  117. }
  118. spin_unlock_irqrestore(&c->fifo_lock, flags);
  119. spin_lock_irqsave(&c->fifo_lock, hf_flags);
  120. list_for_each_entry_safe(mbo, tmp, &c->halt_fifo, list) {
  121. list_del(&mbo->list);
  122. spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
  123. most_free_mbo_coherent(mbo);
  124. spin_lock_irqsave(&c->fifo_lock, hf_flags);
  125. }
  126. spin_unlock_irqrestore(&c->fifo_lock, hf_flags);
  127. if (unlikely((!list_empty(&c->fifo) || !list_empty(&c->halt_fifo))))
  128. dev_warn(&c->dev, "Channel or trash fifo not empty\n");
  129. }
  130. /**
  131. * flush_trash_fifo - clear the trash fifo
  132. * @c: pointer to channel object
  133. */
  134. static int flush_trash_fifo(struct most_channel *c)
  135. {
  136. struct mbo *mbo, *tmp;
  137. unsigned long flags;
  138. spin_lock_irqsave(&c->fifo_lock, flags);
  139. list_for_each_entry_safe(mbo, tmp, &c->trash_fifo, list) {
  140. list_del(&mbo->list);
  141. spin_unlock_irqrestore(&c->fifo_lock, flags);
  142. most_free_mbo_coherent(mbo);
  143. spin_lock_irqsave(&c->fifo_lock, flags);
  144. }
  145. spin_unlock_irqrestore(&c->fifo_lock, flags);
  146. return 0;
  147. }
  148. static ssize_t available_directions_show(struct device *dev,
  149. struct device_attribute *attr,
  150. char *buf)
  151. {
  152. struct most_channel *c = to_channel(dev);
  153. unsigned int i = c->channel_id;
  154. strcpy(buf, "");
  155. if (c->iface->channel_vector[i].direction & MOST_CH_RX)
  156. strcat(buf, "rx ");
  157. if (c->iface->channel_vector[i].direction & MOST_CH_TX)
  158. strcat(buf, "tx ");
  159. strcat(buf, "\n");
  160. return strlen(buf);
  161. }
  162. static ssize_t available_datatypes_show(struct device *dev,
  163. struct device_attribute *attr,
  164. char *buf)
  165. {
  166. struct most_channel *c = to_channel(dev);
  167. unsigned int i = c->channel_id;
  168. strcpy(buf, "");
  169. if (c->iface->channel_vector[i].data_type & MOST_CH_CONTROL)
  170. strcat(buf, "control ");
  171. if (c->iface->channel_vector[i].data_type & MOST_CH_ASYNC)
  172. strcat(buf, "async ");
  173. if (c->iface->channel_vector[i].data_type & MOST_CH_SYNC)
  174. strcat(buf, "sync ");
  175. if (c->iface->channel_vector[i].data_type & MOST_CH_ISOC)
  176. strcat(buf, "isoc ");
  177. strcat(buf, "\n");
  178. return strlen(buf);
  179. }
  180. static ssize_t number_of_packet_buffers_show(struct device *dev,
  181. struct device_attribute *attr,
  182. char *buf)
  183. {
  184. struct most_channel *c = to_channel(dev);
  185. unsigned int i = c->channel_id;
  186. return snprintf(buf, PAGE_SIZE, "%d\n",
  187. c->iface->channel_vector[i].num_buffers_packet);
  188. }
  189. static ssize_t number_of_stream_buffers_show(struct device *dev,
  190. struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct most_channel *c = to_channel(dev);
  194. unsigned int i = c->channel_id;
  195. return snprintf(buf, PAGE_SIZE, "%d\n",
  196. c->iface->channel_vector[i].num_buffers_streaming);
  197. }
  198. static ssize_t size_of_packet_buffer_show(struct device *dev,
  199. struct device_attribute *attr,
  200. char *buf)
  201. {
  202. struct most_channel *c = to_channel(dev);
  203. unsigned int i = c->channel_id;
  204. return snprintf(buf, PAGE_SIZE, "%d\n",
  205. c->iface->channel_vector[i].buffer_size_packet);
  206. }
  207. static ssize_t size_of_stream_buffer_show(struct device *dev,
  208. struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct most_channel *c = to_channel(dev);
  212. unsigned int i = c->channel_id;
  213. return snprintf(buf, PAGE_SIZE, "%d\n",
  214. c->iface->channel_vector[i].buffer_size_streaming);
  215. }
  216. static ssize_t channel_starving_show(struct device *dev,
  217. struct device_attribute *attr,
  218. char *buf)
  219. {
  220. struct most_channel *c = to_channel(dev);
  221. return snprintf(buf, PAGE_SIZE, "%d\n", c->is_starving);
  222. }
  223. static ssize_t set_number_of_buffers_show(struct device *dev,
  224. struct device_attribute *attr,
  225. char *buf)
  226. {
  227. struct most_channel *c = to_channel(dev);
  228. return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.num_buffers);
  229. }
  230. static ssize_t set_buffer_size_show(struct device *dev,
  231. struct device_attribute *attr,
  232. char *buf)
  233. {
  234. struct most_channel *c = to_channel(dev);
  235. return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.buffer_size);
  236. }
  237. static ssize_t set_direction_show(struct device *dev,
  238. struct device_attribute *attr,
  239. char *buf)
  240. {
  241. struct most_channel *c = to_channel(dev);
  242. if (c->cfg.direction & MOST_CH_TX)
  243. return snprintf(buf, PAGE_SIZE, "tx\n");
  244. else if (c->cfg.direction & MOST_CH_RX)
  245. return snprintf(buf, PAGE_SIZE, "rx\n");
  246. return snprintf(buf, PAGE_SIZE, "unconfigured\n");
  247. }
  248. static ssize_t set_datatype_show(struct device *dev,
  249. struct device_attribute *attr,
  250. char *buf)
  251. {
  252. int i;
  253. struct most_channel *c = to_channel(dev);
  254. for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
  255. if (c->cfg.data_type & ch_data_type[i].most_ch_data_type)
  256. return snprintf(buf, PAGE_SIZE, "%s",
  257. ch_data_type[i].name);
  258. }
  259. return snprintf(buf, PAGE_SIZE, "unconfigured\n");
  260. }
  261. static ssize_t set_subbuffer_size_show(struct device *dev,
  262. struct device_attribute *attr,
  263. char *buf)
  264. {
  265. struct most_channel *c = to_channel(dev);
  266. return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.subbuffer_size);
  267. }
  268. static ssize_t set_packets_per_xact_show(struct device *dev,
  269. struct device_attribute *attr,
  270. char *buf)
  271. {
  272. struct most_channel *c = to_channel(dev);
  273. return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.packets_per_xact);
  274. }
  275. static ssize_t set_dbr_size_show(struct device *dev,
  276. struct device_attribute *attr, char *buf)
  277. {
  278. struct most_channel *c = to_channel(dev);
  279. return snprintf(buf, PAGE_SIZE, "%d\n", c->cfg.dbr_size);
  280. }
  281. #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
  282. static umode_t channel_attr_is_visible(struct kobject *kobj,
  283. struct attribute *attr, int index)
  284. {
  285. struct device_attribute *dev_attr = to_dev_attr(attr);
  286. struct device *dev = kobj_to_dev(kobj);
  287. struct most_channel *c = to_channel(dev);
  288. if (!strcmp(dev_attr->attr.name, "set_dbr_size") &&
  289. (c->iface->interface != ITYPE_MEDIALB_DIM2))
  290. return 0;
  291. if (!strcmp(dev_attr->attr.name, "set_packets_per_xact") &&
  292. (c->iface->interface != ITYPE_USB))
  293. return 0;
  294. return attr->mode;
  295. }
  296. #define DEV_ATTR(_name) (&dev_attr_##_name.attr)
  297. static DEVICE_ATTR_RO(available_directions);
  298. static DEVICE_ATTR_RO(available_datatypes);
  299. static DEVICE_ATTR_RO(number_of_packet_buffers);
  300. static DEVICE_ATTR_RO(number_of_stream_buffers);
  301. static DEVICE_ATTR_RO(size_of_stream_buffer);
  302. static DEVICE_ATTR_RO(size_of_packet_buffer);
  303. static DEVICE_ATTR_RO(channel_starving);
  304. static DEVICE_ATTR_RO(set_buffer_size);
  305. static DEVICE_ATTR_RO(set_number_of_buffers);
  306. static DEVICE_ATTR_RO(set_direction);
  307. static DEVICE_ATTR_RO(set_datatype);
  308. static DEVICE_ATTR_RO(set_subbuffer_size);
  309. static DEVICE_ATTR_RO(set_packets_per_xact);
  310. static DEVICE_ATTR_RO(set_dbr_size);
  311. static struct attribute *channel_attrs[] = {
  312. DEV_ATTR(available_directions),
  313. DEV_ATTR(available_datatypes),
  314. DEV_ATTR(number_of_packet_buffers),
  315. DEV_ATTR(number_of_stream_buffers),
  316. DEV_ATTR(size_of_stream_buffer),
  317. DEV_ATTR(size_of_packet_buffer),
  318. DEV_ATTR(channel_starving),
  319. DEV_ATTR(set_buffer_size),
  320. DEV_ATTR(set_number_of_buffers),
  321. DEV_ATTR(set_direction),
  322. DEV_ATTR(set_datatype),
  323. DEV_ATTR(set_subbuffer_size),
  324. DEV_ATTR(set_packets_per_xact),
  325. DEV_ATTR(set_dbr_size),
  326. NULL,
  327. };
  328. static struct attribute_group channel_attr_group = {
  329. .attrs = channel_attrs,
  330. .is_visible = channel_attr_is_visible,
  331. };
  332. static const struct attribute_group *channel_attr_groups[] = {
  333. &channel_attr_group,
  334. NULL,
  335. };
  336. static ssize_t description_show(struct device *dev,
  337. struct device_attribute *attr,
  338. char *buf)
  339. {
  340. struct most_interface *iface = dev_get_drvdata(dev);
  341. return snprintf(buf, PAGE_SIZE, "%s\n", iface->description);
  342. }
  343. static ssize_t interface_show(struct device *dev,
  344. struct device_attribute *attr,
  345. char *buf)
  346. {
  347. struct most_interface *iface = dev_get_drvdata(dev);
  348. switch (iface->interface) {
  349. case ITYPE_LOOPBACK:
  350. return snprintf(buf, PAGE_SIZE, "loopback\n");
  351. case ITYPE_I2C:
  352. return snprintf(buf, PAGE_SIZE, "i2c\n");
  353. case ITYPE_I2S:
  354. return snprintf(buf, PAGE_SIZE, "i2s\n");
  355. case ITYPE_TSI:
  356. return snprintf(buf, PAGE_SIZE, "tsi\n");
  357. case ITYPE_HBI:
  358. return snprintf(buf, PAGE_SIZE, "hbi\n");
  359. case ITYPE_MEDIALB_DIM:
  360. return snprintf(buf, PAGE_SIZE, "mlb_dim\n");
  361. case ITYPE_MEDIALB_DIM2:
  362. return snprintf(buf, PAGE_SIZE, "mlb_dim2\n");
  363. case ITYPE_USB:
  364. return snprintf(buf, PAGE_SIZE, "usb\n");
  365. case ITYPE_PCIE:
  366. return snprintf(buf, PAGE_SIZE, "pcie\n");
  367. }
  368. return snprintf(buf, PAGE_SIZE, "unknown\n");
  369. }
  370. static DEVICE_ATTR_RO(description);
  371. static DEVICE_ATTR_RO(interface);
  372. static struct attribute *interface_attrs[] = {
  373. DEV_ATTR(description),
  374. DEV_ATTR(interface),
  375. NULL,
  376. };
  377. static struct attribute_group interface_attr_group = {
  378. .attrs = interface_attrs,
  379. };
  380. static const struct attribute_group *interface_attr_groups[] = {
  381. &interface_attr_group,
  382. NULL,
  383. };
  384. static struct most_component *match_component(char *name)
  385. {
  386. struct most_component *comp;
  387. list_for_each_entry(comp, &comp_list, list) {
  388. if (!strcmp(comp->name, name))
  389. return comp;
  390. }
  391. return NULL;
  392. }
  393. struct show_links_data {
  394. int offs;
  395. char *buf;
  396. };
  397. static int print_links(struct device *dev, void *data)
  398. {
  399. struct show_links_data *d = data;
  400. int offs = d->offs;
  401. char *buf = d->buf;
  402. struct most_channel *c;
  403. struct most_interface *iface = dev_get_drvdata(dev);
  404. list_for_each_entry(c, &iface->p->channel_list, list) {
  405. if (c->pipe0.comp) {
  406. offs += scnprintf(buf + offs,
  407. PAGE_SIZE - offs,
  408. "%s:%s:%s\n",
  409. c->pipe0.comp->name,
  410. dev_name(iface->dev),
  411. dev_name(&c->dev));
  412. }
  413. if (c->pipe1.comp) {
  414. offs += scnprintf(buf + offs,
  415. PAGE_SIZE - offs,
  416. "%s:%s:%s\n",
  417. c->pipe1.comp->name,
  418. dev_name(iface->dev),
  419. dev_name(&c->dev));
  420. }
  421. }
  422. d->offs = offs;
  423. return 0;
  424. }
  425. static int most_match(struct device *dev, struct device_driver *drv)
  426. {
  427. if (!strcmp(dev_name(dev), "most"))
  428. return 0;
  429. else
  430. return 1;
  431. }
  432. static struct bus_type mostbus = {
  433. .name = "most",
  434. .match = most_match,
  435. };
  436. static ssize_t links_show(struct device_driver *drv, char *buf)
  437. {
  438. struct show_links_data d = { .buf = buf };
  439. bus_for_each_dev(&mostbus, NULL, &d, print_links);
  440. return d.offs;
  441. }
  442. static ssize_t components_show(struct device_driver *drv, char *buf)
  443. {
  444. struct most_component *comp;
  445. int offs = 0;
  446. list_for_each_entry(comp, &comp_list, list) {
  447. offs += scnprintf(buf + offs, PAGE_SIZE - offs, "%s\n",
  448. comp->name);
  449. }
  450. return offs;
  451. }
  452. /**
  453. * get_channel - get pointer to channel
  454. * @mdev: name of the device interface
  455. * @mdev_ch: name of channel
  456. */
  457. static struct most_channel *get_channel(char *mdev, char *mdev_ch)
  458. {
  459. struct device *dev = NULL;
  460. struct most_interface *iface;
  461. struct most_channel *c, *tmp;
  462. dev = bus_find_device_by_name(&mostbus, NULL, mdev);
  463. if (!dev)
  464. return NULL;
  465. put_device(dev);
  466. iface = dev_get_drvdata(dev);
  467. list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
  468. if (!strcmp(dev_name(&c->dev), mdev_ch))
  469. return c;
  470. }
  471. return NULL;
  472. }
  473. static
  474. inline int link_channel_to_component(struct most_channel *c,
  475. struct most_component *comp,
  476. char *name,
  477. char *comp_param)
  478. {
  479. int ret;
  480. struct most_component **comp_ptr;
  481. if (!c->pipe0.comp)
  482. comp_ptr = &c->pipe0.comp;
  483. else if (!c->pipe1.comp)
  484. comp_ptr = &c->pipe1.comp;
  485. else
  486. return -ENOSPC;
  487. *comp_ptr = comp;
  488. ret = comp->probe_channel(c->iface, c->channel_id, &c->cfg, name,
  489. comp_param);
  490. if (ret) {
  491. *comp_ptr = NULL;
  492. return ret;
  493. }
  494. return 0;
  495. }
  496. int most_set_cfg_buffer_size(char *mdev, char *mdev_ch, u16 val)
  497. {
  498. struct most_channel *c = get_channel(mdev, mdev_ch);
  499. if (!c)
  500. return -ENODEV;
  501. c->cfg.buffer_size = val;
  502. return 0;
  503. }
  504. int most_set_cfg_subbuffer_size(char *mdev, char *mdev_ch, u16 val)
  505. {
  506. struct most_channel *c = get_channel(mdev, mdev_ch);
  507. if (!c)
  508. return -ENODEV;
  509. c->cfg.subbuffer_size = val;
  510. return 0;
  511. }
  512. int most_set_cfg_dbr_size(char *mdev, char *mdev_ch, u16 val)
  513. {
  514. struct most_channel *c = get_channel(mdev, mdev_ch);
  515. if (!c)
  516. return -ENODEV;
  517. c->cfg.dbr_size = val;
  518. return 0;
  519. }
  520. int most_set_cfg_num_buffers(char *mdev, char *mdev_ch, u16 val)
  521. {
  522. struct most_channel *c = get_channel(mdev, mdev_ch);
  523. if (!c)
  524. return -ENODEV;
  525. c->cfg.num_buffers = val;
  526. return 0;
  527. }
  528. int most_set_cfg_datatype(char *mdev, char *mdev_ch, char *buf)
  529. {
  530. int i;
  531. struct most_channel *c = get_channel(mdev, mdev_ch);
  532. if (!c)
  533. return -ENODEV;
  534. for (i = 0; i < ARRAY_SIZE(ch_data_type); i++) {
  535. if (!strcmp(buf, ch_data_type[i].name)) {
  536. c->cfg.data_type = ch_data_type[i].most_ch_data_type;
  537. break;
  538. }
  539. }
  540. if (i == ARRAY_SIZE(ch_data_type))
  541. dev_warn(&c->dev, "Invalid attribute settings\n");
  542. return 0;
  543. }
  544. int most_set_cfg_direction(char *mdev, char *mdev_ch, char *buf)
  545. {
  546. struct most_channel *c = get_channel(mdev, mdev_ch);
  547. if (!c)
  548. return -ENODEV;
  549. if (!strcmp(buf, "dir_rx")) {
  550. c->cfg.direction = MOST_CH_RX;
  551. } else if (!strcmp(buf, "rx")) {
  552. c->cfg.direction = MOST_CH_RX;
  553. } else if (!strcmp(buf, "dir_tx")) {
  554. c->cfg.direction = MOST_CH_TX;
  555. } else if (!strcmp(buf, "tx")) {
  556. c->cfg.direction = MOST_CH_TX;
  557. } else {
  558. dev_err(&c->dev, "Invalid direction\n");
  559. return -ENODATA;
  560. }
  561. return 0;
  562. }
  563. int most_set_cfg_packets_xact(char *mdev, char *mdev_ch, u16 val)
  564. {
  565. struct most_channel *c = get_channel(mdev, mdev_ch);
  566. if (!c)
  567. return -ENODEV;
  568. c->cfg.packets_per_xact = val;
  569. return 0;
  570. }
  571. int most_cfg_complete(char *comp_name)
  572. {
  573. struct most_component *comp;
  574. comp = match_component(comp_name);
  575. if (!comp)
  576. return -ENODEV;
  577. return comp->cfg_complete();
  578. }
  579. int most_add_link(char *mdev, char *mdev_ch, char *comp_name, char *link_name,
  580. char *comp_param)
  581. {
  582. struct most_channel *c = get_channel(mdev, mdev_ch);
  583. struct most_component *comp = match_component(comp_name);
  584. if (!c || !comp)
  585. return -ENODEV;
  586. return link_channel_to_component(c, comp, link_name, comp_param);
  587. }
  588. int most_remove_link(char *mdev, char *mdev_ch, char *comp_name)
  589. {
  590. struct most_channel *c;
  591. struct most_component *comp;
  592. comp = match_component(comp_name);
  593. if (!comp)
  594. return -ENODEV;
  595. c = get_channel(mdev, mdev_ch);
  596. if (!c)
  597. return -ENODEV;
  598. if (comp->disconnect_channel(c->iface, c->channel_id))
  599. return -EIO;
  600. if (c->pipe0.comp == comp)
  601. c->pipe0.comp = NULL;
  602. if (c->pipe1.comp == comp)
  603. c->pipe1.comp = NULL;
  604. return 0;
  605. }
  606. #define DRV_ATTR(_name) (&driver_attr_##_name.attr)
  607. static DRIVER_ATTR_RO(links);
  608. static DRIVER_ATTR_RO(components);
  609. static struct attribute *mc_attrs[] = {
  610. DRV_ATTR(links),
  611. DRV_ATTR(components),
  612. NULL,
  613. };
  614. static struct attribute_group mc_attr_group = {
  615. .attrs = mc_attrs,
  616. };
  617. static const struct attribute_group *mc_attr_groups[] = {
  618. &mc_attr_group,
  619. NULL,
  620. };
  621. static struct device_driver mostbus_driver = {
  622. .name = "most_core",
  623. .bus = &mostbus,
  624. .groups = mc_attr_groups,
  625. };
  626. static inline void trash_mbo(struct mbo *mbo)
  627. {
  628. unsigned long flags;
  629. struct most_channel *c = mbo->context;
  630. spin_lock_irqsave(&c->fifo_lock, flags);
  631. list_add(&mbo->list, &c->trash_fifo);
  632. spin_unlock_irqrestore(&c->fifo_lock, flags);
  633. }
  634. static bool hdm_mbo_ready(struct most_channel *c)
  635. {
  636. bool empty;
  637. if (c->enqueue_halt)
  638. return false;
  639. spin_lock_irq(&c->fifo_lock);
  640. empty = list_empty(&c->halt_fifo);
  641. spin_unlock_irq(&c->fifo_lock);
  642. return !empty;
  643. }
  644. static void nq_hdm_mbo(struct mbo *mbo)
  645. {
  646. unsigned long flags;
  647. struct most_channel *c = mbo->context;
  648. spin_lock_irqsave(&c->fifo_lock, flags);
  649. list_add_tail(&mbo->list, &c->halt_fifo);
  650. spin_unlock_irqrestore(&c->fifo_lock, flags);
  651. wake_up_interruptible(&c->hdm_fifo_wq);
  652. }
  653. static int hdm_enqueue_thread(void *data)
  654. {
  655. struct most_channel *c = data;
  656. struct mbo *mbo;
  657. int ret;
  658. typeof(c->iface->enqueue) enqueue = c->iface->enqueue;
  659. while (likely(!kthread_should_stop())) {
  660. wait_event_interruptible(c->hdm_fifo_wq,
  661. hdm_mbo_ready(c) ||
  662. kthread_should_stop());
  663. mutex_lock(&c->nq_mutex);
  664. spin_lock_irq(&c->fifo_lock);
  665. if (unlikely(c->enqueue_halt || list_empty(&c->halt_fifo))) {
  666. spin_unlock_irq(&c->fifo_lock);
  667. mutex_unlock(&c->nq_mutex);
  668. continue;
  669. }
  670. mbo = list_pop_mbo(&c->halt_fifo);
  671. spin_unlock_irq(&c->fifo_lock);
  672. if (c->cfg.direction == MOST_CH_RX)
  673. mbo->buffer_length = c->cfg.buffer_size;
  674. ret = enqueue(mbo->ifp, mbo->hdm_channel_id, mbo);
  675. mutex_unlock(&c->nq_mutex);
  676. if (unlikely(ret)) {
  677. dev_err(&c->dev, "Buffer enqueue failed\n");
  678. nq_hdm_mbo(mbo);
  679. c->hdm_enqueue_task = NULL;
  680. return 0;
  681. }
  682. }
  683. return 0;
  684. }
  685. static int run_enqueue_thread(struct most_channel *c, int channel_id)
  686. {
  687. struct task_struct *task =
  688. kthread_run(hdm_enqueue_thread, c, "hdm_fifo_%d",
  689. channel_id);
  690. if (IS_ERR(task))
  691. return PTR_ERR(task);
  692. c->hdm_enqueue_task = task;
  693. return 0;
  694. }
  695. /**
  696. * arm_mbo - recycle MBO for further usage
  697. * @mbo: most buffer
  698. *
  699. * This puts an MBO back to the list to have it ready for up coming
  700. * tx transactions.
  701. *
  702. * In case the MBO belongs to a channel that recently has been
  703. * poisoned, the MBO is scheduled to be trashed.
  704. * Calls the completion handler of an attached component.
  705. */
  706. static void arm_mbo(struct mbo *mbo)
  707. {
  708. unsigned long flags;
  709. struct most_channel *c;
  710. c = mbo->context;
  711. if (c->is_poisoned) {
  712. trash_mbo(mbo);
  713. return;
  714. }
  715. spin_lock_irqsave(&c->fifo_lock, flags);
  716. ++*mbo->num_buffers_ptr;
  717. list_add_tail(&mbo->list, &c->fifo);
  718. spin_unlock_irqrestore(&c->fifo_lock, flags);
  719. if (c->pipe0.refs && c->pipe0.comp->tx_completion)
  720. c->pipe0.comp->tx_completion(c->iface, c->channel_id);
  721. if (c->pipe1.refs && c->pipe1.comp->tx_completion)
  722. c->pipe1.comp->tx_completion(c->iface, c->channel_id);
  723. }
  724. /**
  725. * arm_mbo_chain - helper function that arms an MBO chain for the HDM
  726. * @c: pointer to interface channel
  727. * @dir: direction of the channel
  728. * @compl: pointer to completion function
  729. *
  730. * This allocates buffer objects including the containing DMA coherent
  731. * buffer and puts them in the fifo.
  732. * Buffers of Rx channels are put in the kthread fifo, hence immediately
  733. * submitted to the HDM.
  734. *
  735. * Returns the number of allocated and enqueued MBOs.
  736. */
  737. static int arm_mbo_chain(struct most_channel *c, int dir,
  738. void (*compl)(struct mbo *))
  739. {
  740. unsigned int i;
  741. struct mbo *mbo;
  742. unsigned long flags;
  743. u32 coherent_buf_size = c->cfg.buffer_size + c->cfg.extra_len;
  744. atomic_set(&c->mbo_nq_level, 0);
  745. for (i = 0; i < c->cfg.num_buffers; i++) {
  746. mbo = kzalloc(sizeof(*mbo), GFP_KERNEL);
  747. if (!mbo)
  748. goto flush_fifos;
  749. mbo->context = c;
  750. mbo->ifp = c->iface;
  751. mbo->hdm_channel_id = c->channel_id;
  752. if (c->iface->dma_alloc) {
  753. mbo->virt_address =
  754. c->iface->dma_alloc(mbo, coherent_buf_size);
  755. } else {
  756. mbo->virt_address =
  757. kzalloc(coherent_buf_size, GFP_KERNEL);
  758. }
  759. if (!mbo->virt_address)
  760. goto release_mbo;
  761. mbo->complete = compl;
  762. mbo->num_buffers_ptr = &dummy_num_buffers;
  763. if (dir == MOST_CH_RX) {
  764. nq_hdm_mbo(mbo);
  765. atomic_inc(&c->mbo_nq_level);
  766. } else {
  767. spin_lock_irqsave(&c->fifo_lock, flags);
  768. list_add_tail(&mbo->list, &c->fifo);
  769. spin_unlock_irqrestore(&c->fifo_lock, flags);
  770. }
  771. }
  772. return c->cfg.num_buffers;
  773. release_mbo:
  774. kfree(mbo);
  775. flush_fifos:
  776. flush_channel_fifos(c);
  777. return 0;
  778. }
  779. /**
  780. * most_submit_mbo - submits an MBO to fifo
  781. * @mbo: most buffer
  782. */
  783. void most_submit_mbo(struct mbo *mbo)
  784. {
  785. if (WARN_ONCE(!mbo || !mbo->context,
  786. "Bad buffer or missing channel reference\n"))
  787. return;
  788. nq_hdm_mbo(mbo);
  789. }
  790. EXPORT_SYMBOL_GPL(most_submit_mbo);
  791. /**
  792. * most_write_completion - write completion handler
  793. * @mbo: most buffer
  794. *
  795. * This recycles the MBO for further usage. In case the channel has been
  796. * poisoned, the MBO is scheduled to be trashed.
  797. */
  798. static void most_write_completion(struct mbo *mbo)
  799. {
  800. struct most_channel *c;
  801. c = mbo->context;
  802. if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE)))
  803. trash_mbo(mbo);
  804. else
  805. arm_mbo(mbo);
  806. }
  807. int channel_has_mbo(struct most_interface *iface, int id,
  808. struct most_component *comp)
  809. {
  810. struct most_channel *c = iface->p->channel[id];
  811. unsigned long flags;
  812. int empty;
  813. if (unlikely(!c))
  814. return -EINVAL;
  815. if (c->pipe0.refs && c->pipe1.refs &&
  816. ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
  817. (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
  818. return 0;
  819. spin_lock_irqsave(&c->fifo_lock, flags);
  820. empty = list_empty(&c->fifo);
  821. spin_unlock_irqrestore(&c->fifo_lock, flags);
  822. return !empty;
  823. }
  824. EXPORT_SYMBOL_GPL(channel_has_mbo);
  825. /**
  826. * most_get_mbo - get pointer to an MBO of pool
  827. * @iface: pointer to interface instance
  828. * @id: channel ID
  829. * @comp: driver component
  830. *
  831. * This attempts to get a free buffer out of the channel fifo.
  832. * Returns a pointer to MBO on success or NULL otherwise.
  833. */
  834. struct mbo *most_get_mbo(struct most_interface *iface, int id,
  835. struct most_component *comp)
  836. {
  837. struct mbo *mbo;
  838. struct most_channel *c;
  839. unsigned long flags;
  840. int *num_buffers_ptr;
  841. c = iface->p->channel[id];
  842. if (unlikely(!c))
  843. return NULL;
  844. if (c->pipe0.refs && c->pipe1.refs &&
  845. ((comp == c->pipe0.comp && c->pipe0.num_buffers <= 0) ||
  846. (comp == c->pipe1.comp && c->pipe1.num_buffers <= 0)))
  847. return NULL;
  848. if (comp == c->pipe0.comp)
  849. num_buffers_ptr = &c->pipe0.num_buffers;
  850. else if (comp == c->pipe1.comp)
  851. num_buffers_ptr = &c->pipe1.num_buffers;
  852. else
  853. num_buffers_ptr = &dummy_num_buffers;
  854. spin_lock_irqsave(&c->fifo_lock, flags);
  855. if (list_empty(&c->fifo)) {
  856. spin_unlock_irqrestore(&c->fifo_lock, flags);
  857. return NULL;
  858. }
  859. mbo = list_pop_mbo(&c->fifo);
  860. --*num_buffers_ptr;
  861. spin_unlock_irqrestore(&c->fifo_lock, flags);
  862. mbo->num_buffers_ptr = num_buffers_ptr;
  863. mbo->buffer_length = c->cfg.buffer_size;
  864. return mbo;
  865. }
  866. EXPORT_SYMBOL_GPL(most_get_mbo);
  867. /**
  868. * most_put_mbo - return buffer to pool
  869. * @mbo: most buffer
  870. */
  871. void most_put_mbo(struct mbo *mbo)
  872. {
  873. struct most_channel *c = mbo->context;
  874. if (c->cfg.direction == MOST_CH_TX) {
  875. arm_mbo(mbo);
  876. return;
  877. }
  878. nq_hdm_mbo(mbo);
  879. atomic_inc(&c->mbo_nq_level);
  880. }
  881. EXPORT_SYMBOL_GPL(most_put_mbo);
  882. /**
  883. * most_read_completion - read completion handler
  884. * @mbo: most buffer
  885. *
  886. * This function is called by the HDM when data has been received from the
  887. * hardware and copied to the buffer of the MBO.
  888. *
  889. * In case the channel has been poisoned it puts the buffer in the trash queue.
  890. * Otherwise, it passes the buffer to an component for further processing.
  891. */
  892. static void most_read_completion(struct mbo *mbo)
  893. {
  894. struct most_channel *c = mbo->context;
  895. if (unlikely(c->is_poisoned || (mbo->status == MBO_E_CLOSE))) {
  896. trash_mbo(mbo);
  897. return;
  898. }
  899. if (mbo->status == MBO_E_INVAL) {
  900. nq_hdm_mbo(mbo);
  901. atomic_inc(&c->mbo_nq_level);
  902. return;
  903. }
  904. if (atomic_sub_and_test(1, &c->mbo_nq_level))
  905. c->is_starving = 1;
  906. if (c->pipe0.refs && c->pipe0.comp->rx_completion &&
  907. c->pipe0.comp->rx_completion(mbo) == 0)
  908. return;
  909. if (c->pipe1.refs && c->pipe1.comp->rx_completion &&
  910. c->pipe1.comp->rx_completion(mbo) == 0)
  911. return;
  912. most_put_mbo(mbo);
  913. }
  914. /**
  915. * most_start_channel - prepares a channel for communication
  916. * @iface: pointer to interface instance
  917. * @id: channel ID
  918. * @comp: driver component
  919. *
  920. * This prepares the channel for usage. Cross-checks whether the
  921. * channel's been properly configured.
  922. *
  923. * Returns 0 on success or error code otherwise.
  924. */
  925. int most_start_channel(struct most_interface *iface, int id,
  926. struct most_component *comp)
  927. {
  928. int num_buffer;
  929. int ret;
  930. struct most_channel *c = iface->p->channel[id];
  931. if (unlikely(!c))
  932. return -EINVAL;
  933. mutex_lock(&c->start_mutex);
  934. if (c->pipe0.refs + c->pipe1.refs > 0)
  935. goto out; /* already started by another component */
  936. if (!try_module_get(iface->mod)) {
  937. dev_err(&c->dev, "Failed to acquire HDM lock\n");
  938. mutex_unlock(&c->start_mutex);
  939. return -ENOLCK;
  940. }
  941. c->cfg.extra_len = 0;
  942. if (c->iface->configure(c->iface, c->channel_id, &c->cfg)) {
  943. dev_err(&c->dev, "Channel configuration failed. Go check settings...\n");
  944. ret = -EINVAL;
  945. goto err_put_module;
  946. }
  947. init_waitqueue_head(&c->hdm_fifo_wq);
  948. if (c->cfg.direction == MOST_CH_RX)
  949. num_buffer = arm_mbo_chain(c, c->cfg.direction,
  950. most_read_completion);
  951. else
  952. num_buffer = arm_mbo_chain(c, c->cfg.direction,
  953. most_write_completion);
  954. if (unlikely(!num_buffer)) {
  955. ret = -ENOMEM;
  956. goto err_put_module;
  957. }
  958. ret = run_enqueue_thread(c, id);
  959. if (ret)
  960. goto err_put_module;
  961. c->is_starving = 0;
  962. c->pipe0.num_buffers = c->cfg.num_buffers / 2;
  963. c->pipe1.num_buffers = c->cfg.num_buffers - c->pipe0.num_buffers;
  964. atomic_set(&c->mbo_ref, num_buffer);
  965. out:
  966. if (comp == c->pipe0.comp)
  967. c->pipe0.refs++;
  968. if (comp == c->pipe1.comp)
  969. c->pipe1.refs++;
  970. mutex_unlock(&c->start_mutex);
  971. return 0;
  972. err_put_module:
  973. module_put(iface->mod);
  974. mutex_unlock(&c->start_mutex);
  975. return ret;
  976. }
  977. EXPORT_SYMBOL_GPL(most_start_channel);
  978. /**
  979. * most_stop_channel - stops a running channel
  980. * @iface: pointer to interface instance
  981. * @id: channel ID
  982. * @comp: driver component
  983. */
  984. int most_stop_channel(struct most_interface *iface, int id,
  985. struct most_component *comp)
  986. {
  987. struct most_channel *c;
  988. if (unlikely((!iface) || (id >= iface->num_channels) || (id < 0))) {
  989. pr_err("Bad interface or index out of range\n");
  990. return -EINVAL;
  991. }
  992. c = iface->p->channel[id];
  993. if (unlikely(!c))
  994. return -EINVAL;
  995. mutex_lock(&c->start_mutex);
  996. if (c->pipe0.refs + c->pipe1.refs >= 2)
  997. goto out;
  998. if (c->hdm_enqueue_task)
  999. kthread_stop(c->hdm_enqueue_task);
  1000. c->hdm_enqueue_task = NULL;
  1001. if (iface->mod)
  1002. module_put(iface->mod);
  1003. c->is_poisoned = true;
  1004. if (c->iface->poison_channel(c->iface, c->channel_id)) {
  1005. dev_err(&c->dev, "Failed to stop channel %d of interface %s\n", c->channel_id,
  1006. c->iface->description);
  1007. mutex_unlock(&c->start_mutex);
  1008. return -EAGAIN;
  1009. }
  1010. flush_trash_fifo(c);
  1011. flush_channel_fifos(c);
  1012. #ifdef CMPL_INTERRUPTIBLE
  1013. if (wait_for_completion_interruptible(&c->cleanup)) {
  1014. dev_err(&c->dev, "Interrupted while cleaning up channel %d\n", c->channel_id);
  1015. mutex_unlock(&c->start_mutex);
  1016. return -EINTR;
  1017. }
  1018. #else
  1019. wait_for_completion(&c->cleanup);
  1020. #endif
  1021. c->is_poisoned = false;
  1022. out:
  1023. if (comp == c->pipe0.comp)
  1024. c->pipe0.refs--;
  1025. if (comp == c->pipe1.comp)
  1026. c->pipe1.refs--;
  1027. mutex_unlock(&c->start_mutex);
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL_GPL(most_stop_channel);
  1031. /**
  1032. * most_register_component - registers a driver component with the core
  1033. * @comp: driver component
  1034. */
  1035. int most_register_component(struct most_component *comp)
  1036. {
  1037. if (!comp) {
  1038. pr_err("Bad component\n");
  1039. return -EINVAL;
  1040. }
  1041. list_add_tail(&comp->list, &comp_list);
  1042. return 0;
  1043. }
  1044. EXPORT_SYMBOL_GPL(most_register_component);
  1045. static int disconnect_channels(struct device *dev, void *data)
  1046. {
  1047. struct most_interface *iface;
  1048. struct most_channel *c, *tmp;
  1049. struct most_component *comp = data;
  1050. iface = dev_get_drvdata(dev);
  1051. list_for_each_entry_safe(c, tmp, &iface->p->channel_list, list) {
  1052. if (c->pipe0.comp == comp || c->pipe1.comp == comp)
  1053. comp->disconnect_channel(c->iface, c->channel_id);
  1054. if (c->pipe0.comp == comp)
  1055. c->pipe0.comp = NULL;
  1056. if (c->pipe1.comp == comp)
  1057. c->pipe1.comp = NULL;
  1058. }
  1059. return 0;
  1060. }
  1061. /**
  1062. * most_deregister_component - deregisters a driver component with the core
  1063. * @comp: driver component
  1064. */
  1065. int most_deregister_component(struct most_component *comp)
  1066. {
  1067. if (!comp) {
  1068. pr_err("Bad component\n");
  1069. return -EINVAL;
  1070. }
  1071. bus_for_each_dev(&mostbus, NULL, comp, disconnect_channels);
  1072. list_del(&comp->list);
  1073. return 0;
  1074. }
  1075. EXPORT_SYMBOL_GPL(most_deregister_component);
  1076. static void release_channel(struct device *dev)
  1077. {
  1078. struct most_channel *c = to_channel(dev);
  1079. kfree(c);
  1080. }
  1081. /**
  1082. * most_register_interface - registers an interface with core
  1083. * @iface: device interface
  1084. *
  1085. * Allocates and initializes a new interface instance and all of its channels.
  1086. * Returns a pointer to kobject or an error pointer.
  1087. */
  1088. int most_register_interface(struct most_interface *iface)
  1089. {
  1090. unsigned int i;
  1091. int id;
  1092. struct most_channel *c;
  1093. if (!iface || !iface->enqueue || !iface->configure ||
  1094. !iface->poison_channel || (iface->num_channels > MAX_CHANNELS))
  1095. return -EINVAL;
  1096. id = ida_simple_get(&mdev_id, 0, 0, GFP_KERNEL);
  1097. if (id < 0) {
  1098. dev_err(iface->dev, "Failed to allocate device ID\n");
  1099. return id;
  1100. }
  1101. iface->p = kzalloc(sizeof(*iface->p), GFP_KERNEL);
  1102. if (!iface->p) {
  1103. ida_simple_remove(&mdev_id, id);
  1104. return -ENOMEM;
  1105. }
  1106. INIT_LIST_HEAD(&iface->p->channel_list);
  1107. iface->p->dev_id = id;
  1108. strscpy(iface->p->name, iface->description, sizeof(iface->p->name));
  1109. iface->dev->bus = &mostbus;
  1110. iface->dev->groups = interface_attr_groups;
  1111. dev_set_drvdata(iface->dev, iface);
  1112. if (device_register(iface->dev)) {
  1113. dev_err(iface->dev, "Failed to register interface device\n");
  1114. kfree(iface->p);
  1115. put_device(iface->dev);
  1116. ida_simple_remove(&mdev_id, id);
  1117. return -ENOMEM;
  1118. }
  1119. for (i = 0; i < iface->num_channels; i++) {
  1120. const char *name_suffix = iface->channel_vector[i].name_suffix;
  1121. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1122. if (!c)
  1123. goto err_free_resources;
  1124. if (!name_suffix)
  1125. snprintf(c->name, STRING_SIZE, "ch%d", i);
  1126. else
  1127. snprintf(c->name, STRING_SIZE, "%s", name_suffix);
  1128. c->dev.init_name = c->name;
  1129. c->dev.parent = iface->dev;
  1130. c->dev.groups = channel_attr_groups;
  1131. c->dev.release = release_channel;
  1132. iface->p->channel[i] = c;
  1133. c->is_starving = 0;
  1134. c->iface = iface;
  1135. c->channel_id = i;
  1136. c->keep_mbo = false;
  1137. c->enqueue_halt = false;
  1138. c->is_poisoned = false;
  1139. c->cfg.direction = 0;
  1140. c->cfg.data_type = 0;
  1141. c->cfg.num_buffers = 0;
  1142. c->cfg.buffer_size = 0;
  1143. c->cfg.subbuffer_size = 0;
  1144. c->cfg.packets_per_xact = 0;
  1145. spin_lock_init(&c->fifo_lock);
  1146. INIT_LIST_HEAD(&c->fifo);
  1147. INIT_LIST_HEAD(&c->trash_fifo);
  1148. INIT_LIST_HEAD(&c->halt_fifo);
  1149. init_completion(&c->cleanup);
  1150. atomic_set(&c->mbo_ref, 0);
  1151. mutex_init(&c->start_mutex);
  1152. mutex_init(&c->nq_mutex);
  1153. list_add_tail(&c->list, &iface->p->channel_list);
  1154. if (device_register(&c->dev)) {
  1155. dev_err(&c->dev, "Failed to register channel device\n");
  1156. goto err_free_most_channel;
  1157. }
  1158. }
  1159. most_interface_register_notify(iface->description);
  1160. return 0;
  1161. err_free_most_channel:
  1162. put_device(&c->dev);
  1163. err_free_resources:
  1164. while (i > 0) {
  1165. c = iface->p->channel[--i];
  1166. device_unregister(&c->dev);
  1167. }
  1168. kfree(iface->p);
  1169. device_unregister(iface->dev);
  1170. ida_simple_remove(&mdev_id, id);
  1171. return -ENOMEM;
  1172. }
  1173. EXPORT_SYMBOL_GPL(most_register_interface);
  1174. /**
  1175. * most_deregister_interface - deregisters an interface with core
  1176. * @iface: device interface
  1177. *
  1178. * Before removing an interface instance from the list, all running
  1179. * channels are stopped and poisoned.
  1180. */
  1181. void most_deregister_interface(struct most_interface *iface)
  1182. {
  1183. int i;
  1184. struct most_channel *c;
  1185. for (i = 0; i < iface->num_channels; i++) {
  1186. c = iface->p->channel[i];
  1187. if (c->pipe0.comp)
  1188. c->pipe0.comp->disconnect_channel(c->iface,
  1189. c->channel_id);
  1190. if (c->pipe1.comp)
  1191. c->pipe1.comp->disconnect_channel(c->iface,
  1192. c->channel_id);
  1193. c->pipe0.comp = NULL;
  1194. c->pipe1.comp = NULL;
  1195. list_del(&c->list);
  1196. device_unregister(&c->dev);
  1197. }
  1198. ida_simple_remove(&mdev_id, iface->p->dev_id);
  1199. kfree(iface->p);
  1200. device_unregister(iface->dev);
  1201. }
  1202. EXPORT_SYMBOL_GPL(most_deregister_interface);
  1203. /**
  1204. * most_stop_enqueue - prevents core from enqueueing MBOs
  1205. * @iface: pointer to interface
  1206. * @id: channel id
  1207. *
  1208. * This is called by an HDM that _cannot_ attend to its duties and
  1209. * is imminent to get run over by the core. The core is not going to
  1210. * enqueue any further packets unless the flagging HDM calls
  1211. * most_resume enqueue().
  1212. */
  1213. void most_stop_enqueue(struct most_interface *iface, int id)
  1214. {
  1215. struct most_channel *c = iface->p->channel[id];
  1216. if (!c)
  1217. return;
  1218. mutex_lock(&c->nq_mutex);
  1219. c->enqueue_halt = true;
  1220. mutex_unlock(&c->nq_mutex);
  1221. }
  1222. EXPORT_SYMBOL_GPL(most_stop_enqueue);
  1223. /**
  1224. * most_resume_enqueue - allow core to enqueue MBOs again
  1225. * @iface: pointer to interface
  1226. * @id: channel id
  1227. *
  1228. * This clears the enqueue halt flag and enqueues all MBOs currently
  1229. * sitting in the wait fifo.
  1230. */
  1231. void most_resume_enqueue(struct most_interface *iface, int id)
  1232. {
  1233. struct most_channel *c = iface->p->channel[id];
  1234. if (!c)
  1235. return;
  1236. mutex_lock(&c->nq_mutex);
  1237. c->enqueue_halt = false;
  1238. mutex_unlock(&c->nq_mutex);
  1239. wake_up_interruptible(&c->hdm_fifo_wq);
  1240. }
  1241. EXPORT_SYMBOL_GPL(most_resume_enqueue);
  1242. static int __init most_init(void)
  1243. {
  1244. int err;
  1245. INIT_LIST_HEAD(&comp_list);
  1246. ida_init(&mdev_id);
  1247. err = bus_register(&mostbus);
  1248. if (err) {
  1249. pr_err("Failed to register most bus\n");
  1250. return err;
  1251. }
  1252. err = driver_register(&mostbus_driver);
  1253. if (err) {
  1254. pr_err("Failed to register core driver\n");
  1255. goto err_unregister_bus;
  1256. }
  1257. configfs_init();
  1258. return 0;
  1259. err_unregister_bus:
  1260. bus_unregister(&mostbus);
  1261. return err;
  1262. }
  1263. static void __exit most_exit(void)
  1264. {
  1265. driver_unregister(&mostbus_driver);
  1266. bus_unregister(&mostbus);
  1267. ida_destroy(&mdev_id);
  1268. }
  1269. subsys_initcall(most_init);
  1270. module_exit(most_exit);
  1271. MODULE_LICENSE("GPL");
  1272. MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
  1273. MODULE_DESCRIPTION("Core module of stacked MOST Linux driver");