1// SPDX-License-Identifier: GPL-2.0
2/*
3 *  Probe for 8250/16550-type ISAPNP serial ports.
4 *
5 *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 *
7 *  Copyright (C) 2001 Russell King, All Rights Reserved.
8 *
9 *  Ported to the Linux PnP Layer - (C) Adam Belay.
10 */
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/pnp.h>
14#include <linux/string.h>
15#include <linux/kernel.h>
16#include <linux/property.h>
17#include <linux/serial_core.h>
18#include <linux/bitops.h>
19
20#include <asm/byteorder.h>
21
22#include "8250.h"
23
24#define UNKNOWN_DEV 0x3000
25#define CIR_PORT	0x0800
26
27static const struct pnp_device_id pnp_dev_table[] = {
28	/* Archtek America Corp. */
29	/* Archtek SmartLink Modem 3334BT Plug & Play */
30	{	"AAC000F",		0	},
31	/* Anchor Datacomm BV */
32	/* SXPro 144 External Data Fax Modem Plug & Play */
33	{	"ADC0001",		0	},
34	/* SXPro 288 External Data Fax Modem Plug & Play */
35	{	"ADC0002",		0	},
36	/* PROLiNK 1456VH ISA PnP K56flex Fax Modem */
37	{	"AEI0250",		0	},
38	/* Actiontec ISA PNP 56K X2 Fax Modem */
39	{	"AEI1240",		0	},
40	/* Rockwell 56K ACF II Fax+Data+Voice Modem */
41	{	"AKY1021",		0 /*SPCI_FL_NO_SHIRQ*/	},
42	/*
43	 * ALi Fast Infrared Controller
44	 * Native driver (ali-ircc) is broken so at least
45	 * it can be used with irtty-sir.
46	 */
47	{	"ALI5123",		0	},
48	/* AZT3005 PnP SOUND DEVICE */
49	{	"AZT4001",		0	},
50	/* Best Data Products Inc. Smart One 336F PnP Modem */
51	{	"BDP3336",		0	},
52	/*  Boca Research */
53	/* Boca Complete Ofc Communicator 14.4 Data-FAX */
54	{	"BRI0A49",		0	},
55	/* Boca Research 33,600 ACF Modem */
56	{	"BRI1400",		0	},
57	/* Boca 33.6 Kbps Internal FD34FSVD */
58	{	"BRI3400",		0	},
59	/* Boca 33.6 Kbps Internal FD34FSVD */
60	{	"BRI0A49",		0	},
61	/* Best Data Products Inc. Smart One 336F PnP Modem */
62	{	"BDP3336",		0	},
63	/* Computer Peripherals Inc */
64	/* EuroViVa CommCenter-33.6 SP PnP */
65	{	"CPI4050",		0	},
66	/* Creative Labs */
67	/* Creative Labs Phone Blaster 28.8 DSVD PnP Voice */
68	{	"CTL3001",		0	},
69	/* Creative Labs Modem Blaster 28.8 DSVD PnP Voice */
70	{	"CTL3011",		0	},
71	/* Davicom ISA 33.6K Modem */
72	{	"DAV0336",		0	},
73	/* Creative */
74	/* Creative Modem Blaster Flash56 DI5601-1 */
75	{	"DMB1032",		0	},
76	/* Creative Modem Blaster V.90 DI5660 */
77	{	"DMB2001",		0	},
78	/* E-Tech */
79	/* E-Tech CyberBULLET PC56RVP */
80	{	"ETT0002",		0	},
81	/* FUJITSU */
82	/* Fujitsu 33600 PnP-I2 R Plug & Play */
83	{	"FUJ0202",		0	},
84	/* Fujitsu FMV-FX431 Plug & Play */
85	{	"FUJ0205",		0	},
86	/* Fujitsu 33600 PnP-I4 R Plug & Play */
87	{	"FUJ0206",		0	},
88	/* Fujitsu Fax Voice 33600 PNP-I5 R Plug & Play */
89	{	"FUJ0209",		0	},
90	/* Archtek America Corp. */
91	/* Archtek SmartLink Modem 3334BT Plug & Play */
92	{	"GVC000F",		0	},
93	/* Archtek SmartLink Modem 3334BRV 33.6K Data Fax Voice */
94	{	"GVC0303",		0	},
95	/* Hayes */
96	/* Hayes Optima 288 V.34-V.FC + FAX + Voice Plug & Play */
97	{	"HAY0001",		0	},
98	/* Hayes Optima 336 V.34 + FAX + Voice PnP */
99	{	"HAY000C",		0	},
100	/* Hayes Optima 336B V.34 + FAX + Voice PnP */
101	{	"HAY000D",		0	},
102	/* Hayes Accura 56K Ext Fax Modem PnP */
103	{	"HAY5670",		0	},
104	/* Hayes Accura 56K Ext Fax Modem PnP */
105	{	"HAY5674",		0	},
106	/* Hayes Accura 56K Fax Modem PnP */
107	{	"HAY5675",		0	},
108	/* Hayes 288, V.34 + FAX */
109	{	"HAYF000",		0	},
110	/* Hayes Optima 288 V.34 + FAX + Voice, Plug & Play */
111	{	"HAYF001",		0	},
112	/* IBM */
113	/* IBM Thinkpad 701 Internal Modem Voice */
114	{	"IBM0033",		0	},
115	/* Intermec */
116	/* Intermec CV60 touchscreen port */
117	{	"PNP4972",		0	},
118	/* Intertex */
119	/* Intertex 28k8 33k6 Voice EXT PnP */
120	{	"IXDC801",		0	},
121	/* Intertex 33k6 56k Voice EXT PnP */
122	{	"IXDC901",		0	},
123	/* Intertex 28k8 33k6 Voice SP EXT PnP */
124	{	"IXDD801",		0	},
125	/* Intertex 33k6 56k Voice SP EXT PnP */
126	{	"IXDD901",		0	},
127	/* Intertex 28k8 33k6 Voice SP INT PnP */
128	{	"IXDF401",		0	},
129	/* Intertex 28k8 33k6 Voice SP EXT PnP */
130	{	"IXDF801",		0	},
131	/* Intertex 33k6 56k Voice SP EXT PnP */
132	{	"IXDF901",		0	},
133	/* Kortex International */
134	/* KORTEX 28800 Externe PnP */
135	{	"KOR4522",		0	},
136	/* KXPro 33.6 Vocal ASVD PnP */
137	{	"KORF661",		0	},
138	/* Lasat */
139	/* LASAT Internet 33600 PnP */
140	{	"LAS4040",		0	},
141	/* Lasat Safire 560 PnP */
142	{	"LAS4540",		0	},
143	/* Lasat Safire 336  PnP */
144	{	"LAS5440",		0	},
145	/* Microcom, Inc. */
146	/* Microcom TravelPorte FAST V.34 Plug & Play */
147	{	"MNP0281",		0	},
148	/* Microcom DeskPorte V.34 FAST or FAST+ Plug & Play */
149	{	"MNP0336",		0	},
150	/* Microcom DeskPorte FAST EP 28.8 Plug & Play */
151	{	"MNP0339",		0	},
152	/* Microcom DeskPorte 28.8P Plug & Play */
153	{	"MNP0342",		0	},
154	/* Microcom DeskPorte FAST ES 28.8 Plug & Play */
155	{	"MNP0500",		0	},
156	/* Microcom DeskPorte FAST ES 28.8 Plug & Play */
157	{	"MNP0501",		0	},
158	/* Microcom DeskPorte 28.8S Internal Plug & Play */
159	{	"MNP0502",		0	},
160	/* Motorola */
161	/* Motorola BitSURFR Plug & Play */
162	{	"MOT1105",		0	},
163	/* Motorola TA210 Plug & Play */
164	{	"MOT1111",		0	},
165	/* Motorola HMTA 200 (ISDN) Plug & Play */
166	{	"MOT1114",		0	},
167	/* Motorola BitSURFR Plug & Play */
168	{	"MOT1115",		0	},
169	/* Motorola Lifestyle 28.8 Internal */
170	{	"MOT1190",		0	},
171	/* Motorola V.3400 Plug & Play */
172	{	"MOT1501",		0	},
173	/* Motorola Lifestyle 28.8 V.34 Plug & Play */
174	{	"MOT1502",		0	},
175	/* Motorola Power 28.8 V.34 Plug & Play */
176	{	"MOT1505",		0	},
177	/* Motorola ModemSURFR External 28.8 Plug & Play */
178	{	"MOT1509",		0	},
179	/* Motorola Premier 33.6 Desktop Plug & Play */
180	{	"MOT150A",		0	},
181	/* Motorola VoiceSURFR 56K External PnP */
182	{	"MOT150F",		0	},
183	/* Motorola ModemSURFR 56K External PnP */
184	{	"MOT1510",		0	},
185	/* Motorola ModemSURFR 56K Internal PnP */
186	{	"MOT1550",		0	},
187	/* Motorola ModemSURFR Internal 28.8 Plug & Play */
188	{	"MOT1560",		0	},
189	/* Motorola Premier 33.6 Internal Plug & Play */
190	{	"MOT1580",		0	},
191	/* Motorola OnlineSURFR 28.8 Internal Plug & Play */
192	{	"MOT15B0",		0	},
193	/* Motorola VoiceSURFR 56K Internal PnP */
194	{	"MOT15F0",		0	},
195	/* Com 1 */
196	/*  Deskline K56 Phone System PnP */
197	{	"MVX00A1",		0	},
198	/* PC Rider K56 Phone System PnP */
199	{	"MVX00F2",		0	},
200	/* NEC 98NOTE SPEAKER PHONE FAX MODEM(33600bps) */
201	{	"nEC8241",		0	},
202	/* Pace 56 Voice Internal Plug & Play Modem */
203	{	"PMC2430",		0	},
204	/* Generic */
205	/* Generic standard PC COM port	 */
206	{	"PNP0500",		0	},
207	/* Generic 16550A-compatible COM port */
208	{	"PNP0501",		0	},
209	/* Compaq 14400 Modem */
210	{	"PNPC000",		0	},
211	/* Compaq 2400/9600 Modem */
212	{	"PNPC001",		0	},
213	/* Dial-Up Networking Serial Cable between 2 PCs */
214	{	"PNPC031",		0	},
215	/* Dial-Up Networking Parallel Cable between 2 PCs */
216	{	"PNPC032",		0	},
217	/* Standard 9600 bps Modem */
218	{	"PNPC100",		0	},
219	/* Standard 14400 bps Modem */
220	{	"PNPC101",		0	},
221	/*  Standard 28800 bps Modem*/
222	{	"PNPC102",		0	},
223	/*  Standard Modem*/
224	{	"PNPC103",		0	},
225	/*  Standard 9600 bps Modem*/
226	{	"PNPC104",		0	},
227	/*  Standard 14400 bps Modem*/
228	{	"PNPC105",		0	},
229	/*  Standard 28800 bps Modem*/
230	{	"PNPC106",		0	},
231	/*  Standard Modem */
232	{	"PNPC107",		0	},
233	/* Standard 9600 bps Modem */
234	{	"PNPC108",		0	},
235	/* Standard 14400 bps Modem */
236	{	"PNPC109",		0	},
237	/* Standard 28800 bps Modem */
238	{	"PNPC10A",		0	},
239	/* Standard Modem */
240	{	"PNPC10B",		0	},
241	/* Standard 9600 bps Modem */
242	{	"PNPC10C",		0	},
243	/* Standard 14400 bps Modem */
244	{	"PNPC10D",		0	},
245	/* Standard 28800 bps Modem */
246	{	"PNPC10E",		0	},
247	/* Standard Modem */
248	{	"PNPC10F",		0	},
249	/* Standard PCMCIA Card Modem */
250	{	"PNP2000",		0	},
251	/* Rockwell */
252	/* Modular Technology */
253	/* Rockwell 33.6 DPF Internal PnP */
254	/* Modular Technology 33.6 Internal PnP */
255	{	"ROK0030",		0	},
256	/* Kortex International */
257	/* KORTEX 14400 Externe PnP */
258	{	"ROK0100",		0	},
259	/* Rockwell 28.8 */
260	{	"ROK4120",		0	},
261	/* Viking Components, Inc */
262	/* Viking 28.8 INTERNAL Fax+Data+Voice PnP */
263	{	"ROK4920",		0	},
264	/* Rockwell */
265	/* British Telecom */
266	/* Modular Technology */
267	/* Rockwell 33.6 DPF External PnP */
268	/* BT Prologue 33.6 External PnP */
269	/* Modular Technology 33.6 External PnP */
270	{	"RSS00A0",		0	},
271	/* Viking 56K FAX INT */
272	{	"RSS0262",		0	},
273	/* K56 par,VV,Voice,Speakphone,AudioSpan,PnP */
274	{       "RSS0250",              0       },
275	/* SupraExpress 28.8 Data/Fax PnP modem */
276	{	"SUP1310",		0	},
277	/* SupraExpress 336i PnP Voice Modem */
278	{	"SUP1381",		0	},
279	/* SupraExpress 33.6 Data/Fax PnP modem */
280	{	"SUP1421",		0	},
281	/* SupraExpress 33.6 Data/Fax PnP modem */
282	{	"SUP1590",		0	},
283	/* SupraExpress 336i Sp ASVD */
284	{	"SUP1620",		0	},
285	/* SupraExpress 33.6 Data/Fax PnP modem */
286	{	"SUP1760",		0	},
287	/* SupraExpress 56i Sp Intl */
288	{	"SUP2171",		0	},
289	/* Phoebe Micro */
290	/* Phoebe Micro 33.6 Data Fax 1433VQH Plug & Play */
291	{	"TEX0011",		0	},
292	/* Archtek America Corp. */
293	/* Archtek SmartLink Modem 3334BT Plug & Play */
294	{	"UAC000F",		0	},
295	/* 3Com Corp. */
296	/* Gateway Telepath IIvi 33.6 */
297	{	"USR0000",		0	},
298	/* U.S. Robotics Sporster 33.6K Fax INT PnP */
299	{	"USR0002",		0	},
300	/*  Sportster Vi 14.4 PnP FAX Voicemail */
301	{	"USR0004",		0	},
302	/* U.S. Robotics 33.6K Voice INT PnP */
303	{	"USR0006",		0	},
304	/* U.S. Robotics 33.6K Voice EXT PnP */
305	{	"USR0007",		0	},
306	/* U.S. Robotics Courier V.Everything INT PnP */
307	{	"USR0009",		0	},
308	/* U.S. Robotics 33.6K Voice INT PnP */
309	{	"USR2002",		0	},
310	/* U.S. Robotics 56K Voice INT PnP */
311	{	"USR2070",		0	},
312	/* U.S. Robotics 56K Voice EXT PnP */
313	{	"USR2080",		0	},
314	/* U.S. Robotics 56K FAX INT */
315	{	"USR3031",		0	},
316	/* U.S. Robotics 56K FAX INT */
317	{	"USR3050",		0	},
318	/* U.S. Robotics 56K Voice INT PnP */
319	{	"USR3070",		0	},
320	/* U.S. Robotics 56K Voice EXT PnP */
321	{	"USR3080",		0	},
322	/* U.S. Robotics 56K Voice INT PnP */
323	{	"USR3090",		0	},
324	/* U.S. Robotics 56K Message  */
325	{	"USR9100",		0	},
326	/* U.S. Robotics 56K FAX EXT PnP*/
327	{	"USR9160",		0	},
328	/* U.S. Robotics 56K FAX INT PnP*/
329	{	"USR9170",		0	},
330	/* U.S. Robotics 56K Voice EXT PnP*/
331	{	"USR9180",		0	},
332	/* U.S. Robotics 56K Voice INT PnP*/
333	{	"USR9190",		0	},
334	/* Wacom tablets */
335	{	"WACFXXX",		0	},
336	/* Compaq touchscreen */
337	{       "FPI2002",              0 },
338	/* Fujitsu Stylistic touchscreens */
339	{       "FUJ02B2",              0 },
340	{       "FUJ02B3",              0 },
341	/* Fujitsu Stylistic LT touchscreens */
342	{       "FUJ02B4",              0 },
343	/* Passive Fujitsu Stylistic touchscreens */
344	{       "FUJ02B6",              0 },
345	{       "FUJ02B7",              0 },
346	{       "FUJ02B8",              0 },
347	{       "FUJ02B9",              0 },
348	{       "FUJ02BC",              0 },
349	/* Fujitsu Wacom Tablet PC device */
350	{	"FUJ02E5",		0	},
351	/* Fujitsu P-series tablet PC device */
352	{	"FUJ02E6",		0	},
353	/* Fujitsu Wacom 2FGT Tablet PC device */
354	{	"FUJ02E7",		0	},
355	/* Fujitsu Wacom 1FGT Tablet PC device */
356	{	"FUJ02E9",		0	},
357	/*
358	 * LG C1 EXPRESS DUAL (C1-PB11A3) touch screen (actually a FUJ02E6
359	 * in disguise).
360	 */
361	{	"LTS0001",		0       },
362	/* Rockwell's (PORALiNK) 33600 INT PNP */
363	{	"WCI0003",		0	},
364	/* Unknown PnP modems */
365	{	"PNPCXXX",		UNKNOWN_DEV	},
366	/* More unknown PnP modems */
367	{	"PNPDXXX",		UNKNOWN_DEV	},
368	/*
369	 * Winbond CIR port, should not be probed. We should keep track of
370	 * it to prevent the legacy serial driver from probing it.
371	 */
372	{	"WEC1022",		CIR_PORT	},
373	/*
374	 * SMSC IrCC SIR/FIR port, should not be probed by serial driver as
375	 * well so its own driver can bind to it.
376	 */
377	{	"SMCF010",		CIR_PORT	},
378	{	"",			0	}
379};
380
381MODULE_DEVICE_TABLE(pnp, pnp_dev_table);
382
383static const char *modem_names[] = {
384	"MODEM", "Modem", "modem", "FAX", "Fax", "fax",
385	"56K", "56k", "K56", "33.6", "28.8", "14.4",
386	"33,600", "28,800", "14,400", "33.600", "28.800", "14.400",
387	"33600", "28800", "14400", "V.90", "V.34", "V.32", NULL
388};
389
390static bool check_name(const char *name)
391{
392	const char **tmp;
393
394	for (tmp = modem_names; *tmp; tmp++)
395		if (strstr(name, *tmp))
396			return true;
397
398	return false;
399}
400
401static bool check_resources(struct pnp_dev *dev)
402{
403	static const resource_size_t base[] = {0x2f8, 0x3f8, 0x2e8, 0x3e8};
404	unsigned int i;
405
406	for (i = 0; i < ARRAY_SIZE(base); i++) {
407		if (pnp_possible_config(dev, IORESOURCE_IO, base[i], 8))
408			return true;
409	}
410
411	return false;
412}
413
414/*
415 * Given a complete unknown PnP device, try to use some heuristics to
416 * detect modems. Currently use such heuristic set:
417 *     - dev->name or dev->bus->name must contain "modem" substring;
418 *     - device must have only one IO region (8 byte long) with base address
419 *       0x2e8, 0x3e8, 0x2f8 or 0x3f8.
420 *
421 * Such detection looks very ugly, but can detect at least some of numerous
422 * PnP modems, alternatively we must hardcode all modems in pnp_devices[]
423 * table.
424 */
425static int serial_pnp_guess_board(struct pnp_dev *dev)
426{
427	if (!(check_name(pnp_dev_name(dev)) ||
428	    (dev->card && check_name(dev->card->name))))
429		return -ENODEV;
430
431	if (check_resources(dev))
432		return 0;
433
434	return -ENODEV;
435}
436
437static int
438serial_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
439{
440	struct uart_8250_port uart, *port;
441	int ret, line, flags = dev_id->driver_data;
442
443	if (flags & UNKNOWN_DEV) {
444		ret = serial_pnp_guess_board(dev);
445		if (ret < 0)
446			return ret;
447	}
448
449	memset(&uart, 0, sizeof(uart));
450	if (pnp_irq_valid(dev, 0))
451		uart.port.irq = pnp_irq(dev, 0);
452	if ((flags & CIR_PORT) && pnp_port_valid(dev, 2)) {
453		uart.port.iobase = pnp_port_start(dev, 2);
454		uart.port.iotype = UPIO_PORT;
455	} else if (pnp_port_valid(dev, 0)) {
456		uart.port.iobase = pnp_port_start(dev, 0);
457		uart.port.iotype = UPIO_PORT;
458	} else if (pnp_mem_valid(dev, 0)) {
459		uart.port.mapbase = pnp_mem_start(dev, 0);
460		uart.port.iotype = UPIO_MEM;
461		uart.port.flags = UPF_IOREMAP;
462	} else
463		return -ENODEV;
464
465	dev_dbg(&dev->dev,
466		 "Setup PNP port: port %#lx, mem %#llx, irq %u, type %u\n",
467		 uart.port.iobase, (unsigned long long)uart.port.mapbase,
468		 uart.port.irq, uart.port.iotype);
469
470	if (flags & CIR_PORT) {
471		uart.port.flags |= UPF_FIXED_PORT | UPF_FIXED_TYPE;
472		uart.port.type = PORT_8250_CIR;
473	}
474
475	uart.port.flags |= UPF_SKIP_TEST | UPF_BOOT_AUTOCONF;
476	if (pnp_irq_flags(dev, 0) & IORESOURCE_IRQ_SHAREABLE)
477		uart.port.flags |= UPF_SHARE_IRQ;
478	if (device_property_read_u32(&dev->dev, "clock-frequency", &uart.port.uartclk)) {
479		uart.port.uartclk = 1843200;
480	}
481	uart.port.dev = &dev->dev;
482
483	line = serial8250_register_8250_port(&uart);
484	if (line < 0 || (flags & CIR_PORT))
485		return -ENODEV;
486
487	port = serial8250_get_port(line);
488	if (uart_console(&port->port))
489		dev->capabilities |= PNP_CONSOLE;
490
491	pnp_set_drvdata(dev, (void *)((long)line + 1));
492	return 0;
493}
494
495static void serial_pnp_remove(struct pnp_dev *dev)
496{
497	long line = (long)pnp_get_drvdata(dev);
498
499	dev->capabilities &= ~PNP_CONSOLE;
500	if (line)
501		serial8250_unregister_port(line - 1);
502}
503
504static int __maybe_unused serial_pnp_suspend(struct device *dev)
505{
506	long line = (long)dev_get_drvdata(dev);
507
508	if (!line)
509		return -ENODEV;
510	serial8250_suspend_port(line - 1);
511	return 0;
512}
513
514static int __maybe_unused serial_pnp_resume(struct device *dev)
515{
516	long line = (long)dev_get_drvdata(dev);
517
518	if (!line)
519		return -ENODEV;
520	serial8250_resume_port(line - 1);
521	return 0;
522}
523
524static SIMPLE_DEV_PM_OPS(serial_pnp_pm_ops, serial_pnp_suspend, serial_pnp_resume);
525
526static struct pnp_driver serial_pnp_driver = {
527	.name		= "serial",
528	.probe		= serial_pnp_probe,
529	.remove		= serial_pnp_remove,
530	.driver         = {
531		.pm     = &serial_pnp_pm_ops,
532	},
533	.id_table	= pnp_dev_table,
534};
535
536int serial8250_pnp_init(void)
537{
538	return pnp_register_driver(&serial_pnp_driver);
539}
540
541void serial8250_pnp_exit(void)
542{
543	pnp_unregister_driver(&serial_pnp_driver);
544}
545
546