wwan_core.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* WWAN Driver Core
  3. *
  4. * Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org>
  5. * Copyright (c) 2025, Sergey Ryazanov <ryazanov.s.a@gmail.com>
  6. */
  7. #include <linux/bitmap.h>
  8. #include <linux/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/fs.h>
  12. #include <linux/init.h>
  13. #include <linux/idr.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/poll.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/termios.h>
  22. #include <linux/gnss.h>
  23. #include <linux/wwan.h>
  24. #include <net/rtnetlink.h>
  25. #include <uapi/linux/wwan.h>
  26. /* Maximum number of minors in use */
  27. #define WWAN_MAX_MINORS (1 << MINORBITS)
  28. static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
  29. static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
  30. static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
  31. static const struct class wwan_class = {
  32. .name = "wwan",
  33. };
  34. static int wwan_major;
  35. static struct dentry *wwan_debugfs_dir;
  36. #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
  37. #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
  38. /* WWAN port flags */
  39. #define WWAN_PORT_TX_OFF 0
  40. /**
  41. * struct wwan_device - The structure that defines a WWAN device
  42. *
  43. * @id: WWAN device unique ID.
  44. * @refcount: Reference count of this WWAN device. When this refcount reaches
  45. * zero, the device is deleted. NB: access is protected by global
  46. * wwan_register_lock mutex.
  47. * @dev: Underlying device.
  48. * @ops: wwan device ops
  49. * @ops_ctxt: context to pass to ops
  50. * @debugfs_dir: WWAN device debugfs dir
  51. */
  52. struct wwan_device {
  53. unsigned int id;
  54. int refcount;
  55. struct device dev;
  56. const struct wwan_ops *ops;
  57. void *ops_ctxt;
  58. #ifdef CONFIG_WWAN_DEBUGFS
  59. struct dentry *debugfs_dir;
  60. #endif
  61. };
  62. /**
  63. * struct wwan_port - The structure that defines a WWAN port
  64. * @type: Port type
  65. * @start_count: Port start counter
  66. * @flags: Store port state and capabilities
  67. * @ops: Pointer to WWAN port operations
  68. * @ops_lock: Protect port ops
  69. * @dev: Underlying device
  70. * @rxq: Buffer inbound queue
  71. * @waitqueue: The waitqueue for port fops (read/write/poll)
  72. * @data_lock: Port specific data access serialization
  73. * @headroom_len: SKB reserved headroom size
  74. * @frag_len: Length to fragment packet
  75. * @at_data: AT port specific data
  76. * @gnss: Pointer to GNSS device associated with this port
  77. */
  78. struct wwan_port {
  79. enum wwan_port_type type;
  80. unsigned int start_count;
  81. unsigned long flags;
  82. const struct wwan_port_ops *ops;
  83. struct mutex ops_lock; /* Serialize ops + protect against removal */
  84. struct device dev;
  85. struct sk_buff_head rxq;
  86. wait_queue_head_t waitqueue;
  87. struct mutex data_lock; /* Port specific data access serialization */
  88. size_t headroom_len;
  89. size_t frag_len;
  90. union {
  91. struct {
  92. struct ktermios termios;
  93. int mdmbits;
  94. } at_data;
  95. struct gnss_device *gnss;
  96. };
  97. };
  98. static int wwan_port_op_start(struct wwan_port *port);
  99. static void wwan_port_op_stop(struct wwan_port *port);
  100. static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
  101. bool nonblock);
  102. static int wwan_wait_tx(struct wwan_port *port, bool nonblock);
  103. static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
  104. {
  105. struct wwan_device *wwan = to_wwan_dev(dev);
  106. return sprintf(buf, "%d\n", wwan->id);
  107. }
  108. static DEVICE_ATTR_RO(index);
  109. static struct attribute *wwan_dev_attrs[] = {
  110. &dev_attr_index.attr,
  111. NULL,
  112. };
  113. ATTRIBUTE_GROUPS(wwan_dev);
  114. static void wwan_dev_destroy(struct device *dev)
  115. {
  116. struct wwan_device *wwandev = to_wwan_dev(dev);
  117. ida_free(&wwan_dev_ids, wwandev->id);
  118. kfree(wwandev);
  119. }
  120. static const struct device_type wwan_dev_type = {
  121. .name = "wwan_dev",
  122. .release = wwan_dev_destroy,
  123. .groups = wwan_dev_groups,
  124. };
  125. static int wwan_dev_parent_match(struct device *dev, const void *parent)
  126. {
  127. return (dev->type == &wwan_dev_type &&
  128. (dev->parent == parent || dev == parent));
  129. }
  130. static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
  131. {
  132. struct device *dev;
  133. dev = class_find_device(&wwan_class, NULL, parent, wwan_dev_parent_match);
  134. if (!dev)
  135. return ERR_PTR(-ENODEV);
  136. return to_wwan_dev(dev);
  137. }
  138. static int wwan_dev_name_match(struct device *dev, const void *name)
  139. {
  140. return dev->type == &wwan_dev_type &&
  141. strcmp(dev_name(dev), name) == 0;
  142. }
  143. static struct wwan_device *wwan_dev_get_by_name(const char *name)
  144. {
  145. struct device *dev;
  146. dev = class_find_device(&wwan_class, NULL, name, wwan_dev_name_match);
  147. if (!dev)
  148. return ERR_PTR(-ENODEV);
  149. return to_wwan_dev(dev);
  150. }
  151. #ifdef CONFIG_WWAN_DEBUGFS
  152. struct dentry *wwan_get_debugfs_dir(struct device *parent)
  153. {
  154. struct wwan_device *wwandev;
  155. wwandev = wwan_dev_get_by_parent(parent);
  156. if (IS_ERR(wwandev))
  157. return ERR_CAST(wwandev);
  158. return wwandev->debugfs_dir;
  159. }
  160. EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
  161. static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
  162. {
  163. struct wwan_device *wwandev;
  164. if (dev->type != &wwan_dev_type)
  165. return 0;
  166. wwandev = to_wwan_dev(dev);
  167. return wwandev->debugfs_dir == dir;
  168. }
  169. static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
  170. {
  171. struct device *dev;
  172. dev = class_find_device(&wwan_class, NULL, dir, wwan_dev_debugfs_match);
  173. if (!dev)
  174. return ERR_PTR(-ENODEV);
  175. return to_wwan_dev(dev);
  176. }
  177. void wwan_put_debugfs_dir(struct dentry *dir)
  178. {
  179. struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
  180. if (WARN_ON(IS_ERR(wwandev)))
  181. return;
  182. /* wwan_dev_get_by_debugfs() also got a reference */
  183. put_device(&wwandev->dev);
  184. put_device(&wwandev->dev);
  185. }
  186. EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
  187. #endif
  188. /* This function allocates and registers a new WWAN device OR if a WWAN device
  189. * already exist for the given parent, it gets a reference and return it.
  190. * This function is not exported (for now), it is called indirectly via
  191. * wwan_create_port().
  192. */
  193. static struct wwan_device *wwan_create_dev(struct device *parent)
  194. {
  195. struct wwan_device *wwandev;
  196. int err, id;
  197. /* The 'find-alloc-register' operation must be protected against
  198. * concurrent execution, a WWAN device is possibly shared between
  199. * multiple callers or concurrently unregistered from wwan_remove_dev().
  200. */
  201. mutex_lock(&wwan_register_lock);
  202. /* If wwandev already exists, return it */
  203. wwandev = wwan_dev_get_by_parent(parent);
  204. if (!IS_ERR(wwandev)) {
  205. wwandev->refcount++;
  206. goto done_unlock;
  207. }
  208. id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
  209. if (id < 0) {
  210. wwandev = ERR_PTR(id);
  211. goto done_unlock;
  212. }
  213. wwandev = kzalloc_obj(*wwandev);
  214. if (!wwandev) {
  215. wwandev = ERR_PTR(-ENOMEM);
  216. ida_free(&wwan_dev_ids, id);
  217. goto done_unlock;
  218. }
  219. wwandev->dev.parent = parent;
  220. wwandev->dev.class = &wwan_class;
  221. wwandev->dev.type = &wwan_dev_type;
  222. wwandev->id = id;
  223. wwandev->refcount = 1;
  224. dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
  225. err = device_register(&wwandev->dev);
  226. if (err) {
  227. put_device(&wwandev->dev);
  228. wwandev = ERR_PTR(err);
  229. goto done_unlock;
  230. }
  231. #ifdef CONFIG_WWAN_DEBUGFS
  232. wwandev->debugfs_dir =
  233. debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
  234. wwan_debugfs_dir);
  235. #endif
  236. done_unlock:
  237. mutex_unlock(&wwan_register_lock);
  238. return wwandev;
  239. }
  240. static void wwan_remove_dev(struct wwan_device *wwandev)
  241. {
  242. /* Prevent concurrent picking from wwan_create_dev */
  243. mutex_lock(&wwan_register_lock);
  244. if (--wwandev->refcount <= 0) {
  245. struct device *child = device_find_any_child(&wwandev->dev);
  246. put_device(child);
  247. if (WARN_ON(wwandev->ops || child)) /* Paranoid */
  248. goto out_unlock;
  249. #ifdef CONFIG_WWAN_DEBUGFS
  250. debugfs_remove_recursive(wwandev->debugfs_dir);
  251. #endif
  252. device_unregister(&wwandev->dev);
  253. } else {
  254. put_device(&wwandev->dev);
  255. }
  256. out_unlock:
  257. mutex_unlock(&wwan_register_lock);
  258. }
  259. /* ------- WWAN port management ------- */
  260. static const struct {
  261. const char * const name; /* Port type name */
  262. const char * const devsuf; /* Port device name suffix */
  263. } wwan_port_types[WWAN_PORT_MAX + 1] = {
  264. [WWAN_PORT_AT] = {
  265. .name = "AT",
  266. .devsuf = "at",
  267. },
  268. [WWAN_PORT_MBIM] = {
  269. .name = "MBIM",
  270. .devsuf = "mbim",
  271. },
  272. [WWAN_PORT_QMI] = {
  273. .name = "QMI",
  274. .devsuf = "qmi",
  275. },
  276. [WWAN_PORT_QCDM] = {
  277. .name = "QCDM",
  278. .devsuf = "qcdm",
  279. },
  280. [WWAN_PORT_FIREHOSE] = {
  281. .name = "FIREHOSE",
  282. .devsuf = "firehose",
  283. },
  284. [WWAN_PORT_XMMRPC] = {
  285. .name = "XMMRPC",
  286. .devsuf = "xmmrpc",
  287. },
  288. [WWAN_PORT_FASTBOOT] = {
  289. .name = "FASTBOOT",
  290. .devsuf = "fastboot",
  291. },
  292. [WWAN_PORT_ADB] = {
  293. .name = "ADB",
  294. .devsuf = "adb",
  295. },
  296. [WWAN_PORT_MIPC] = {
  297. .name = "MIPC",
  298. .devsuf = "mipc",
  299. },
  300. /* WWAN_PORT_NMEA is exported via the GNSS subsystem */
  301. };
  302. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  303. char *buf)
  304. {
  305. struct wwan_port *port = to_wwan_port(dev);
  306. return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
  307. }
  308. static DEVICE_ATTR_RO(type);
  309. static struct attribute *wwan_port_attrs[] = {
  310. &dev_attr_type.attr,
  311. NULL,
  312. };
  313. ATTRIBUTE_GROUPS(wwan_port);
  314. static void wwan_port_destroy(struct device *dev)
  315. {
  316. struct wwan_port *port = to_wwan_port(dev);
  317. if (dev->class == &wwan_class)
  318. ida_free(&minors, MINOR(dev->devt));
  319. mutex_destroy(&port->data_lock);
  320. mutex_destroy(&port->ops_lock);
  321. kfree(port);
  322. }
  323. static const struct device_type wwan_port_dev_type = {
  324. .name = "wwan_port",
  325. .release = wwan_port_destroy,
  326. .groups = wwan_port_groups,
  327. };
  328. static int wwan_port_minor_match(struct device *dev, const void *minor)
  329. {
  330. return (dev->type == &wwan_port_dev_type &&
  331. MINOR(dev->devt) == *(unsigned int *)minor);
  332. }
  333. static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
  334. {
  335. struct device *dev;
  336. dev = class_find_device(&wwan_class, NULL, &minor, wwan_port_minor_match);
  337. if (!dev)
  338. return ERR_PTR(-ENODEV);
  339. return to_wwan_port(dev);
  340. }
  341. /* Allocate and set unique name based on passed format
  342. *
  343. * Name allocation approach is highly inspired by the __dev_alloc_name()
  344. * function.
  345. *
  346. * To avoid names collision, the caller must prevent the new port device
  347. * registration as well as concurrent invocation of this function.
  348. */
  349. static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
  350. {
  351. struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
  352. const unsigned int max_ports = PAGE_SIZE * 8;
  353. struct class_dev_iter iter;
  354. unsigned long *idmap;
  355. struct device *dev;
  356. char buf[0x20];
  357. int id;
  358. idmap = bitmap_zalloc(max_ports, GFP_KERNEL);
  359. if (!idmap)
  360. return -ENOMEM;
  361. /* Collect ids of same name format ports */
  362. class_dev_iter_init(&iter, &wwan_class, NULL, &wwan_port_dev_type);
  363. while ((dev = class_dev_iter_next(&iter))) {
  364. if (dev->parent != &wwandev->dev)
  365. continue;
  366. if (sscanf(dev_name(dev), fmt, &id) != 1)
  367. continue;
  368. if (id < 0 || id >= max_ports)
  369. continue;
  370. set_bit(id, idmap);
  371. }
  372. class_dev_iter_exit(&iter);
  373. /* Allocate unique id */
  374. id = find_first_zero_bit(idmap, max_ports);
  375. bitmap_free(idmap);
  376. snprintf(buf, sizeof(buf), fmt, id); /* Name generation */
  377. dev = device_find_child_by_name(&wwandev->dev, buf);
  378. if (dev) {
  379. put_device(dev);
  380. return -ENFILE;
  381. }
  382. return dev_set_name(&port->dev, "%s", buf);
  383. }
  384. /* Register a regular WWAN port device (e.g. AT, MBIM, etc.) */
  385. static int wwan_port_register_wwan(struct wwan_port *port)
  386. {
  387. struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
  388. char namefmt[0x20];
  389. int minor, err;
  390. /* A port is exposed as character device, get a minor */
  391. minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
  392. if (minor < 0)
  393. return minor;
  394. port->dev.class = &wwan_class;
  395. port->dev.devt = MKDEV(wwan_major, minor);
  396. /* allocate unique name based on wwan device id, port type and number */
  397. snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
  398. wwan_port_types[port->type].devsuf);
  399. /* Serialize ports registration */
  400. mutex_lock(&wwan_register_lock);
  401. __wwan_port_dev_assign_name(port, namefmt);
  402. err = device_add(&port->dev);
  403. mutex_unlock(&wwan_register_lock);
  404. if (err) {
  405. ida_free(&minors, minor);
  406. port->dev.class = NULL;
  407. return err;
  408. }
  409. dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev));
  410. return 0;
  411. }
  412. /* Unregister a regular WWAN port (e.g. AT, MBIM, etc) */
  413. static void wwan_port_unregister_wwan(struct wwan_port *port)
  414. {
  415. struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
  416. dev_set_drvdata(&port->dev, NULL);
  417. dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&port->dev));
  418. device_del(&port->dev);
  419. }
  420. #if IS_ENABLED(CONFIG_GNSS)
  421. static int wwan_gnss_open(struct gnss_device *gdev)
  422. {
  423. return wwan_port_op_start(gnss_get_drvdata(gdev));
  424. }
  425. static void wwan_gnss_close(struct gnss_device *gdev)
  426. {
  427. wwan_port_op_stop(gnss_get_drvdata(gdev));
  428. }
  429. static int wwan_gnss_write(struct gnss_device *gdev, const unsigned char *buf,
  430. size_t count)
  431. {
  432. struct wwan_port *port = gnss_get_drvdata(gdev);
  433. struct sk_buff *skb, *head = NULL, *tail = NULL;
  434. size_t frag_len, remain = count;
  435. int ret;
  436. ret = wwan_wait_tx(port, false);
  437. if (ret)
  438. return ret;
  439. do {
  440. frag_len = min(remain, port->frag_len);
  441. skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
  442. if (!skb) {
  443. ret = -ENOMEM;
  444. goto freeskb;
  445. }
  446. skb_reserve(skb, port->headroom_len);
  447. memcpy(skb_put(skb, frag_len), buf + count - remain, frag_len);
  448. if (!head) {
  449. head = skb;
  450. } else {
  451. if (!tail)
  452. skb_shinfo(head)->frag_list = skb;
  453. else
  454. tail->next = skb;
  455. tail = skb;
  456. head->data_len += skb->len;
  457. head->len += skb->len;
  458. head->truesize += skb->truesize;
  459. }
  460. } while (remain -= frag_len);
  461. ret = wwan_port_op_tx(port, head, false);
  462. if (!ret)
  463. return count;
  464. freeskb:
  465. kfree_skb(head);
  466. return ret;
  467. }
  468. static struct gnss_operations wwan_gnss_ops = {
  469. .open = wwan_gnss_open,
  470. .close = wwan_gnss_close,
  471. .write_raw = wwan_gnss_write,
  472. };
  473. /* GNSS port specific device registration */
  474. static int wwan_port_register_gnss(struct wwan_port *port)
  475. {
  476. struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
  477. struct gnss_device *gdev;
  478. int err;
  479. gdev = gnss_allocate_device(&wwandev->dev);
  480. if (!gdev)
  481. return -ENOMEM;
  482. /* NB: for now we support only NMEA WWAN port type, so hardcode
  483. * the GNSS port type. If more GNSS WWAN port types will be added,
  484. * then we should dynamically map WWAN port type to GNSS type.
  485. */
  486. gdev->type = GNSS_TYPE_NMEA;
  487. gdev->ops = &wwan_gnss_ops;
  488. gnss_set_drvdata(gdev, port);
  489. port->gnss = gdev;
  490. err = gnss_register_device(gdev);
  491. if (err) {
  492. gnss_put_device(gdev);
  493. return err;
  494. }
  495. dev_info(&wwandev->dev, "port %s attached\n", dev_name(&gdev->dev));
  496. return 0;
  497. }
  498. /* GNSS port specific device unregistration */
  499. static void wwan_port_unregister_gnss(struct wwan_port *port)
  500. {
  501. struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
  502. struct gnss_device *gdev = port->gnss;
  503. dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&gdev->dev));
  504. gnss_deregister_device(gdev);
  505. gnss_put_device(gdev);
  506. }
  507. #else
  508. static int wwan_port_register_gnss(struct wwan_port *port)
  509. {
  510. return -EOPNOTSUPP;
  511. }
  512. static void wwan_port_unregister_gnss(struct wwan_port *port)
  513. {
  514. WARN_ON(1); /* This handler cannot be called */
  515. }
  516. #endif
  517. struct wwan_port *wwan_create_port(struct device *parent,
  518. enum wwan_port_type type,
  519. const struct wwan_port_ops *ops,
  520. struct wwan_port_caps *caps,
  521. void *drvdata)
  522. {
  523. struct wwan_device *wwandev;
  524. struct wwan_port *port;
  525. int err;
  526. if (type > WWAN_PORT_MAX || !ops)
  527. return ERR_PTR(-EINVAL);
  528. /* A port is always a child of a WWAN device, retrieve (allocate or
  529. * pick) the WWAN device based on the provided parent device.
  530. */
  531. wwandev = wwan_create_dev(parent);
  532. if (IS_ERR(wwandev))
  533. return ERR_CAST(wwandev);
  534. port = kzalloc_obj(*port);
  535. if (!port) {
  536. err = -ENOMEM;
  537. goto error_wwandev_remove;
  538. }
  539. port->type = type;
  540. port->ops = ops;
  541. port->frag_len = caps ? caps->frag_len : SIZE_MAX;
  542. port->headroom_len = caps ? caps->headroom_len : 0;
  543. mutex_init(&port->ops_lock);
  544. skb_queue_head_init(&port->rxq);
  545. init_waitqueue_head(&port->waitqueue);
  546. mutex_init(&port->data_lock);
  547. port->dev.parent = &wwandev->dev;
  548. port->dev.type = &wwan_port_dev_type;
  549. dev_set_drvdata(&port->dev, drvdata);
  550. device_initialize(&port->dev);
  551. if (port->type == WWAN_PORT_NMEA)
  552. err = wwan_port_register_gnss(port);
  553. else
  554. err = wwan_port_register_wwan(port);
  555. if (err)
  556. goto error_put_device;
  557. return port;
  558. error_put_device:
  559. put_device(&port->dev);
  560. error_wwandev_remove:
  561. wwan_remove_dev(wwandev);
  562. return ERR_PTR(err);
  563. }
  564. EXPORT_SYMBOL_GPL(wwan_create_port);
  565. void wwan_remove_port(struct wwan_port *port)
  566. {
  567. struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
  568. mutex_lock(&port->ops_lock);
  569. if (port->start_count) {
  570. port->ops->stop(port);
  571. port->start_count = 0;
  572. }
  573. port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
  574. mutex_unlock(&port->ops_lock);
  575. wake_up_interruptible(&port->waitqueue);
  576. skb_queue_purge(&port->rxq);
  577. if (port->type == WWAN_PORT_NMEA)
  578. wwan_port_unregister_gnss(port);
  579. else
  580. wwan_port_unregister_wwan(port);
  581. put_device(&port->dev);
  582. /* Release related wwan device */
  583. wwan_remove_dev(wwandev);
  584. }
  585. EXPORT_SYMBOL_GPL(wwan_remove_port);
  586. void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
  587. {
  588. if (port->type == WWAN_PORT_NMEA) {
  589. #if IS_ENABLED(CONFIG_GNSS)
  590. gnss_insert_raw(port->gnss, skb->data, skb->len);
  591. #endif
  592. consume_skb(skb);
  593. } else {
  594. skb_queue_tail(&port->rxq, skb);
  595. wake_up_interruptible(&port->waitqueue);
  596. }
  597. }
  598. EXPORT_SYMBOL_GPL(wwan_port_rx);
  599. void wwan_port_txon(struct wwan_port *port)
  600. {
  601. clear_bit(WWAN_PORT_TX_OFF, &port->flags);
  602. wake_up_interruptible(&port->waitqueue);
  603. }
  604. EXPORT_SYMBOL_GPL(wwan_port_txon);
  605. void wwan_port_txoff(struct wwan_port *port)
  606. {
  607. set_bit(WWAN_PORT_TX_OFF, &port->flags);
  608. }
  609. EXPORT_SYMBOL_GPL(wwan_port_txoff);
  610. void *wwan_port_get_drvdata(struct wwan_port *port)
  611. {
  612. return dev_get_drvdata(&port->dev);
  613. }
  614. EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
  615. static int wwan_port_op_start(struct wwan_port *port)
  616. {
  617. int ret = 0;
  618. mutex_lock(&port->ops_lock);
  619. if (!port->ops) { /* Port got unplugged */
  620. ret = -ENODEV;
  621. goto out_unlock;
  622. }
  623. /* If port is already started, don't start again */
  624. if (!port->start_count)
  625. ret = port->ops->start(port);
  626. if (!ret)
  627. port->start_count++;
  628. out_unlock:
  629. mutex_unlock(&port->ops_lock);
  630. return ret;
  631. }
  632. static void wwan_port_op_stop(struct wwan_port *port)
  633. {
  634. mutex_lock(&port->ops_lock);
  635. port->start_count--;
  636. if (!port->start_count) {
  637. if (port->ops)
  638. port->ops->stop(port);
  639. skb_queue_purge(&port->rxq);
  640. }
  641. mutex_unlock(&port->ops_lock);
  642. }
  643. static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
  644. bool nonblock)
  645. {
  646. int ret;
  647. mutex_lock(&port->ops_lock);
  648. if (!port->ops) { /* Port got unplugged */
  649. ret = -ENODEV;
  650. goto out_unlock;
  651. }
  652. if (nonblock || !port->ops->tx_blocking)
  653. ret = port->ops->tx(port, skb);
  654. else
  655. ret = port->ops->tx_blocking(port, skb);
  656. out_unlock:
  657. mutex_unlock(&port->ops_lock);
  658. return ret;
  659. }
  660. static bool is_read_blocked(struct wwan_port *port)
  661. {
  662. return skb_queue_empty(&port->rxq) && port->ops;
  663. }
  664. static bool is_write_blocked(struct wwan_port *port)
  665. {
  666. return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
  667. }
  668. static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
  669. {
  670. if (!is_read_blocked(port))
  671. return 0;
  672. if (nonblock)
  673. return -EAGAIN;
  674. if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
  675. return -ERESTARTSYS;
  676. return 0;
  677. }
  678. static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
  679. {
  680. if (!is_write_blocked(port))
  681. return 0;
  682. if (nonblock)
  683. return -EAGAIN;
  684. if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
  685. return -ERESTARTSYS;
  686. return 0;
  687. }
  688. static int wwan_port_fops_open(struct inode *inode, struct file *file)
  689. {
  690. struct wwan_port *port;
  691. int err = 0;
  692. port = wwan_port_get_by_minor(iminor(inode));
  693. if (IS_ERR(port))
  694. return PTR_ERR(port);
  695. file->private_data = port;
  696. stream_open(inode, file);
  697. err = wwan_port_op_start(port);
  698. if (err)
  699. put_device(&port->dev);
  700. return err;
  701. }
  702. static int wwan_port_fops_release(struct inode *inode, struct file *filp)
  703. {
  704. struct wwan_port *port = filp->private_data;
  705. wwan_port_op_stop(port);
  706. put_device(&port->dev);
  707. return 0;
  708. }
  709. static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
  710. size_t count, loff_t *ppos)
  711. {
  712. struct wwan_port *port = filp->private_data;
  713. struct sk_buff *skb;
  714. size_t copied;
  715. int ret;
  716. ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
  717. if (ret)
  718. return ret;
  719. skb = skb_dequeue(&port->rxq);
  720. if (!skb)
  721. return -EIO;
  722. copied = min_t(size_t, count, skb->len);
  723. if (copy_to_user(buf, skb->data, copied)) {
  724. kfree_skb(skb);
  725. return -EFAULT;
  726. }
  727. skb_pull(skb, copied);
  728. /* skb is not fully consumed, keep it in the queue */
  729. if (skb->len)
  730. skb_queue_head(&port->rxq, skb);
  731. else
  732. consume_skb(skb);
  733. return copied;
  734. }
  735. static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
  736. size_t count, loff_t *offp)
  737. {
  738. struct sk_buff *skb, *head = NULL, *tail = NULL;
  739. struct wwan_port *port = filp->private_data;
  740. size_t frag_len, remain = count;
  741. int ret;
  742. ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
  743. if (ret)
  744. return ret;
  745. do {
  746. frag_len = min(remain, port->frag_len);
  747. skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
  748. if (!skb) {
  749. ret = -ENOMEM;
  750. goto freeskb;
  751. }
  752. skb_reserve(skb, port->headroom_len);
  753. if (!head) {
  754. head = skb;
  755. } else if (!tail) {
  756. skb_shinfo(head)->frag_list = skb;
  757. tail = skb;
  758. } else {
  759. tail->next = skb;
  760. tail = skb;
  761. }
  762. if (copy_from_user(skb_put(skb, frag_len), buf + count - remain, frag_len)) {
  763. ret = -EFAULT;
  764. goto freeskb;
  765. }
  766. if (skb != head) {
  767. head->data_len += skb->len;
  768. head->len += skb->len;
  769. head->truesize += skb->truesize;
  770. }
  771. } while (remain -= frag_len);
  772. ret = wwan_port_op_tx(port, head, !!(filp->f_flags & O_NONBLOCK));
  773. if (!ret)
  774. return count;
  775. freeskb:
  776. kfree_skb(head);
  777. return ret;
  778. }
  779. static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
  780. {
  781. struct wwan_port *port = filp->private_data;
  782. __poll_t mask = 0;
  783. poll_wait(filp, &port->waitqueue, wait);
  784. mutex_lock(&port->ops_lock);
  785. if (port->ops && port->ops->tx_poll)
  786. mask |= port->ops->tx_poll(port, filp, wait);
  787. else if (!is_write_blocked(port))
  788. mask |= EPOLLOUT | EPOLLWRNORM;
  789. if (!is_read_blocked(port))
  790. mask |= EPOLLIN | EPOLLRDNORM;
  791. if (!port->ops)
  792. mask |= EPOLLHUP | EPOLLERR;
  793. mutex_unlock(&port->ops_lock);
  794. return mask;
  795. }
  796. /* Implements minimalistic stub terminal IOCTLs support */
  797. static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
  798. unsigned long arg)
  799. {
  800. int ret = 0;
  801. mutex_lock(&port->data_lock);
  802. switch (cmd) {
  803. case TCFLSH:
  804. break;
  805. case TCGETS:
  806. if (copy_to_user((void __user *)arg, &port->at_data.termios,
  807. sizeof(struct termios)))
  808. ret = -EFAULT;
  809. break;
  810. case TCSETS:
  811. case TCSETSW:
  812. case TCSETSF:
  813. if (copy_from_user(&port->at_data.termios, (void __user *)arg,
  814. sizeof(struct termios)))
  815. ret = -EFAULT;
  816. break;
  817. #ifdef TCGETS2
  818. case TCGETS2:
  819. if (copy_to_user((void __user *)arg, &port->at_data.termios,
  820. sizeof(struct termios2)))
  821. ret = -EFAULT;
  822. break;
  823. case TCSETS2:
  824. case TCSETSW2:
  825. case TCSETSF2:
  826. if (copy_from_user(&port->at_data.termios, (void __user *)arg,
  827. sizeof(struct termios2)))
  828. ret = -EFAULT;
  829. break;
  830. #endif
  831. case TIOCMGET:
  832. ret = put_user(port->at_data.mdmbits, (int __user *)arg);
  833. break;
  834. case TIOCMSET:
  835. case TIOCMBIC:
  836. case TIOCMBIS: {
  837. int mdmbits;
  838. if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
  839. ret = -EFAULT;
  840. break;
  841. }
  842. if (cmd == TIOCMBIC)
  843. port->at_data.mdmbits &= ~mdmbits;
  844. else if (cmd == TIOCMBIS)
  845. port->at_data.mdmbits |= mdmbits;
  846. else
  847. port->at_data.mdmbits = mdmbits;
  848. break;
  849. }
  850. default:
  851. ret = -ENOIOCTLCMD;
  852. }
  853. mutex_unlock(&port->data_lock);
  854. return ret;
  855. }
  856. static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
  857. unsigned long arg)
  858. {
  859. struct wwan_port *port = filp->private_data;
  860. int res;
  861. if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
  862. res = wwan_port_fops_at_ioctl(port, cmd, arg);
  863. if (res != -ENOIOCTLCMD)
  864. return res;
  865. }
  866. switch (cmd) {
  867. case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
  868. unsigned long flags;
  869. struct sk_buff *skb;
  870. int amount = 0;
  871. spin_lock_irqsave(&port->rxq.lock, flags);
  872. skb_queue_walk(&port->rxq, skb)
  873. amount += skb->len;
  874. spin_unlock_irqrestore(&port->rxq.lock, flags);
  875. return put_user(amount, (int __user *)arg);
  876. }
  877. default:
  878. return -ENOIOCTLCMD;
  879. }
  880. }
  881. static const struct file_operations wwan_port_fops = {
  882. .owner = THIS_MODULE,
  883. .open = wwan_port_fops_open,
  884. .release = wwan_port_fops_release,
  885. .read = wwan_port_fops_read,
  886. .write = wwan_port_fops_write,
  887. .poll = wwan_port_fops_poll,
  888. .unlocked_ioctl = wwan_port_fops_ioctl,
  889. #ifdef CONFIG_COMPAT
  890. .compat_ioctl = compat_ptr_ioctl,
  891. #endif
  892. .llseek = noop_llseek,
  893. };
  894. static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
  895. struct netlink_ext_ack *extack)
  896. {
  897. if (!data)
  898. return -EINVAL;
  899. if (!tb[IFLA_PARENT_DEV_NAME])
  900. return -EINVAL;
  901. if (!data[IFLA_WWAN_LINK_ID])
  902. return -EINVAL;
  903. return 0;
  904. }
  905. static const struct device_type wwan_type = { .name = "wwan" };
  906. static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
  907. const char *ifname,
  908. unsigned char name_assign_type,
  909. unsigned int num_tx_queues,
  910. unsigned int num_rx_queues)
  911. {
  912. const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
  913. struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
  914. struct net_device *dev;
  915. unsigned int priv_size;
  916. if (IS_ERR(wwandev))
  917. return ERR_CAST(wwandev);
  918. /* only supported if ops were registered (not just ports) */
  919. if (!wwandev->ops) {
  920. dev = ERR_PTR(-EOPNOTSUPP);
  921. goto out;
  922. }
  923. priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
  924. dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
  925. wwandev->ops->setup, num_tx_queues, num_rx_queues);
  926. if (dev) {
  927. SET_NETDEV_DEV(dev, &wwandev->dev);
  928. SET_NETDEV_DEVTYPE(dev, &wwan_type);
  929. }
  930. out:
  931. /* release the reference */
  932. put_device(&wwandev->dev);
  933. return dev;
  934. }
  935. static int wwan_rtnl_newlink(struct net_device *dev,
  936. struct rtnl_newlink_params *params,
  937. struct netlink_ext_ack *extack)
  938. {
  939. struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
  940. struct wwan_netdev_priv *priv = netdev_priv(dev);
  941. struct nlattr **data = params->data;
  942. u32 link_id;
  943. int ret;
  944. link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
  945. if (IS_ERR(wwandev))
  946. return PTR_ERR(wwandev);
  947. /* shouldn't have a netdev (left) with us as parent so WARN */
  948. if (WARN_ON(!wwandev->ops)) {
  949. ret = -EOPNOTSUPP;
  950. goto out;
  951. }
  952. priv->link_id = link_id;
  953. if (wwandev->ops->newlink)
  954. ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
  955. link_id, extack);
  956. else
  957. ret = register_netdevice(dev);
  958. out:
  959. /* release the reference */
  960. put_device(&wwandev->dev);
  961. return ret;
  962. }
  963. static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
  964. {
  965. struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
  966. if (IS_ERR(wwandev))
  967. return;
  968. /* shouldn't have a netdev (left) with us as parent so WARN */
  969. if (WARN_ON(!wwandev->ops))
  970. goto out;
  971. if (wwandev->ops->dellink)
  972. wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
  973. else
  974. unregister_netdevice_queue(dev, head);
  975. out:
  976. /* release the reference */
  977. put_device(&wwandev->dev);
  978. }
  979. static size_t wwan_rtnl_get_size(const struct net_device *dev)
  980. {
  981. return
  982. nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
  983. 0;
  984. }
  985. static int wwan_rtnl_fill_info(struct sk_buff *skb,
  986. const struct net_device *dev)
  987. {
  988. struct wwan_netdev_priv *priv = netdev_priv(dev);
  989. if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
  990. goto nla_put_failure;
  991. return 0;
  992. nla_put_failure:
  993. return -EMSGSIZE;
  994. }
  995. static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
  996. [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
  997. };
  998. static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
  999. .kind = "wwan",
  1000. .maxtype = IFLA_WWAN_MAX,
  1001. .alloc = wwan_rtnl_alloc,
  1002. .validate = wwan_rtnl_validate,
  1003. .newlink = wwan_rtnl_newlink,
  1004. .dellink = wwan_rtnl_dellink,
  1005. .get_size = wwan_rtnl_get_size,
  1006. .fill_info = wwan_rtnl_fill_info,
  1007. .policy = wwan_rtnl_policy,
  1008. };
  1009. static void wwan_create_default_link(struct wwan_device *wwandev,
  1010. u32 def_link_id)
  1011. {
  1012. struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
  1013. struct nlattr *data[IFLA_WWAN_MAX + 1];
  1014. struct rtnl_newlink_params params = {
  1015. .src_net = &init_net,
  1016. .tb = tb,
  1017. .data = data,
  1018. };
  1019. struct net_device *dev;
  1020. struct nlmsghdr *nlh;
  1021. struct sk_buff *msg;
  1022. /* Forge attributes required to create a WWAN netdev. We first
  1023. * build a netlink message and then parse it. This looks
  1024. * odd, but such approach is less error prone.
  1025. */
  1026. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1027. if (WARN_ON(!msg))
  1028. return;
  1029. nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
  1030. if (WARN_ON(!nlh))
  1031. goto free_attrs;
  1032. if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
  1033. goto free_attrs;
  1034. tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
  1035. if (!tb[IFLA_LINKINFO])
  1036. goto free_attrs;
  1037. linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
  1038. if (!linkinfo[IFLA_INFO_DATA])
  1039. goto free_attrs;
  1040. if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
  1041. goto free_attrs;
  1042. nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
  1043. nla_nest_end(msg, tb[IFLA_LINKINFO]);
  1044. nlmsg_end(msg, nlh);
  1045. /* The next three parsing calls can not fail */
  1046. nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
  1047. nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
  1048. NULL, NULL);
  1049. nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
  1050. linkinfo[IFLA_INFO_DATA], NULL, NULL);
  1051. rtnl_lock();
  1052. dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
  1053. &wwan_rtnl_link_ops, tb, NULL);
  1054. if (WARN_ON(IS_ERR(dev)))
  1055. goto unlock;
  1056. if (WARN_ON(wwan_rtnl_newlink(dev, &params, NULL))) {
  1057. free_netdev(dev);
  1058. goto unlock;
  1059. }
  1060. rtnl_configure_link(dev, NULL, 0, NULL); /* Link initialized, notify new link */
  1061. unlock:
  1062. rtnl_unlock();
  1063. free_attrs:
  1064. nlmsg_free(msg);
  1065. }
  1066. /**
  1067. * wwan_register_ops - register WWAN device ops
  1068. * @parent: Device to use as parent and shared by all WWAN ports and
  1069. * created netdevs
  1070. * @ops: operations to register
  1071. * @ctxt: context to pass to operations
  1072. * @def_link_id: id of the default link that will be automatically created by
  1073. * the WWAN core for the WWAN device. The default link will not be created
  1074. * if the passed value is WWAN_NO_DEFAULT_LINK.
  1075. *
  1076. * Returns: 0 on success, a negative error code on failure
  1077. */
  1078. int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
  1079. void *ctxt, u32 def_link_id)
  1080. {
  1081. struct wwan_device *wwandev;
  1082. if (WARN_ON(!parent || !ops || !ops->setup))
  1083. return -EINVAL;
  1084. wwandev = wwan_create_dev(parent);
  1085. if (IS_ERR(wwandev))
  1086. return PTR_ERR(wwandev);
  1087. if (WARN_ON(wwandev->ops)) {
  1088. wwan_remove_dev(wwandev);
  1089. return -EBUSY;
  1090. }
  1091. wwandev->ops = ops;
  1092. wwandev->ops_ctxt = ctxt;
  1093. /* NB: we do not abort ops registration in case of default link
  1094. * creation failure. Link ops is the management interface, while the
  1095. * default link creation is a service option. And we should not prevent
  1096. * a user from manually creating a link latter if service option failed
  1097. * now.
  1098. */
  1099. if (def_link_id != WWAN_NO_DEFAULT_LINK)
  1100. wwan_create_default_link(wwandev, def_link_id);
  1101. return 0;
  1102. }
  1103. EXPORT_SYMBOL_GPL(wwan_register_ops);
  1104. /* Enqueue child netdev deletion */
  1105. static int wwan_child_dellink(struct device *dev, void *data)
  1106. {
  1107. struct list_head *kill_list = data;
  1108. if (dev->type == &wwan_type)
  1109. wwan_rtnl_dellink(to_net_dev(dev), kill_list);
  1110. return 0;
  1111. }
  1112. /**
  1113. * wwan_unregister_ops - remove WWAN device ops
  1114. * @parent: Device to use as parent and shared by all WWAN ports and
  1115. * created netdevs
  1116. */
  1117. void wwan_unregister_ops(struct device *parent)
  1118. {
  1119. struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
  1120. LIST_HEAD(kill_list);
  1121. if (WARN_ON(IS_ERR(wwandev)))
  1122. return;
  1123. if (WARN_ON(!wwandev->ops)) {
  1124. put_device(&wwandev->dev);
  1125. return;
  1126. }
  1127. /* put the reference obtained by wwan_dev_get_by_parent(),
  1128. * we should still have one (that the owner is giving back
  1129. * now) due to the ops being assigned.
  1130. */
  1131. put_device(&wwandev->dev);
  1132. rtnl_lock(); /* Prevent concurrent netdev(s) creation/destroying */
  1133. /* Remove all child netdev(s), using batch removing */
  1134. device_for_each_child(&wwandev->dev, &kill_list,
  1135. wwan_child_dellink);
  1136. unregister_netdevice_many(&kill_list);
  1137. wwandev->ops = NULL; /* Finally remove ops */
  1138. rtnl_unlock();
  1139. wwandev->ops_ctxt = NULL;
  1140. wwan_remove_dev(wwandev);
  1141. }
  1142. EXPORT_SYMBOL_GPL(wwan_unregister_ops);
  1143. static int __init wwan_init(void)
  1144. {
  1145. int err;
  1146. err = rtnl_link_register(&wwan_rtnl_link_ops);
  1147. if (err)
  1148. return err;
  1149. err = class_register(&wwan_class);
  1150. if (err)
  1151. goto unregister;
  1152. /* chrdev used for wwan ports */
  1153. wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
  1154. &wwan_port_fops);
  1155. if (wwan_major < 0) {
  1156. err = wwan_major;
  1157. goto destroy;
  1158. }
  1159. #ifdef CONFIG_WWAN_DEBUGFS
  1160. wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
  1161. #endif
  1162. return 0;
  1163. destroy:
  1164. class_unregister(&wwan_class);
  1165. unregister:
  1166. rtnl_link_unregister(&wwan_rtnl_link_ops);
  1167. return err;
  1168. }
  1169. static void __exit wwan_exit(void)
  1170. {
  1171. debugfs_remove_recursive(wwan_debugfs_dir);
  1172. __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
  1173. rtnl_link_unregister(&wwan_rtnl_link_ops);
  1174. class_unregister(&wwan_class);
  1175. }
  1176. module_init(wwan_init);
  1177. module_exit(wwan_exit);
  1178. MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
  1179. MODULE_DESCRIPTION("WWAN core");
  1180. MODULE_LICENSE("GPL v2");