probe.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Parallel port device probing code
  4. *
  5. * Authors: Carsten Gross, carsten@sol.wohnheim.uni-ulm.de
  6. * Philip Blundell <philb@gnu.org>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/parport.h>
  10. #include <linux/ctype.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <linux/uaccess.h>
  14. static const struct {
  15. const char *token;
  16. const char *descr;
  17. } classes[] = {
  18. { "", "Legacy device" },
  19. { "PRINTER", "Printer" },
  20. { "MODEM", "Modem" },
  21. { "NET", "Network device" },
  22. { "HDC", "Hard disk" },
  23. { "PCMCIA", "PCMCIA" },
  24. { "MEDIA", "Multimedia device" },
  25. { "FDC", "Floppy disk" },
  26. { "PORTS", "Ports" },
  27. { "SCANNER", "Scanner" },
  28. { "DIGICAM", "Digital camera" },
  29. { "", "Unknown device" },
  30. { "", "Unspecified" },
  31. { "SCSIADAPTER", "SCSI adapter" },
  32. { NULL, NULL }
  33. };
  34. static void pretty_print(struct parport *port, int device)
  35. {
  36. struct parport_device_info *info = &port->probe_info[device + 1];
  37. printk(KERN_INFO "%s", port->name);
  38. if (device >= 0)
  39. printk (" (addr %d)", device);
  40. printk (": %s", classes[info->class].descr);
  41. if (info->class)
  42. printk(", %s %s", info->mfr, info->model);
  43. printk("\n");
  44. }
  45. static void parse_data(struct parport *port, int device, char *str)
  46. {
  47. char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
  48. char *p = txt, *q;
  49. int guessed_class = PARPORT_CLASS_UNSPEC;
  50. struct parport_device_info *info = &port->probe_info[device + 1];
  51. if (!txt) {
  52. printk(KERN_WARNING "%s probe: memory squeeze\n", port->name);
  53. return;
  54. }
  55. strcpy(txt, str);
  56. while (p) {
  57. char *sep;
  58. q = strchr(p, ';');
  59. if (q) *q = 0;
  60. sep = strchr(p, ':');
  61. if (sep) {
  62. char *u;
  63. *(sep++) = 0;
  64. /* Get rid of trailing blanks */
  65. u = sep + strlen (sep) - 1;
  66. while (u >= p && *u == ' ')
  67. *u-- = '\0';
  68. u = p;
  69. while (*u) {
  70. *u = toupper(*u);
  71. u++;
  72. }
  73. if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
  74. kfree(info->mfr);
  75. info->mfr = kstrdup(sep, GFP_KERNEL);
  76. } else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
  77. kfree(info->model);
  78. info->model = kstrdup(sep, GFP_KERNEL);
  79. } else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
  80. int i;
  81. kfree(info->class_name);
  82. info->class_name = kstrdup(sep, GFP_KERNEL);
  83. for (u = sep; *u; u++)
  84. *u = toupper(*u);
  85. for (i = 0; classes[i].token; i++) {
  86. if (!strcmp(classes[i].token, sep)) {
  87. info->class = i;
  88. goto rock_on;
  89. }
  90. }
  91. printk(KERN_WARNING "%s probe: warning, class '%s' not understood.\n", port->name, sep);
  92. info->class = PARPORT_CLASS_OTHER;
  93. } else if (!strcmp(p, "CMD") ||
  94. !strcmp(p, "COMMAND SET")) {
  95. kfree(info->cmdset);
  96. info->cmdset = kstrdup(sep, GFP_KERNEL);
  97. /* if it speaks printer language, it's
  98. probably a printer */
  99. if (strstr(sep, "PJL") || strstr(sep, "PCL"))
  100. guessed_class = PARPORT_CLASS_PRINTER;
  101. } else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
  102. kfree(info->description);
  103. info->description = kstrdup(sep, GFP_KERNEL);
  104. }
  105. }
  106. rock_on:
  107. if (q)
  108. p = q + 1;
  109. else
  110. p = NULL;
  111. }
  112. /* If the device didn't tell us its class, maybe we have managed to
  113. guess one from the things it did say. */
  114. if (info->class == PARPORT_CLASS_UNSPEC)
  115. info->class = guessed_class;
  116. pretty_print (port, device);
  117. kfree(txt);
  118. }
  119. /* Read up to count-1 bytes of device id. Terminate buffer with
  120. * '\0'. Buffer begins with two Device ID length bytes as given by
  121. * device. */
  122. static ssize_t parport_read_device_id (struct parport *port, char *buffer,
  123. size_t count)
  124. {
  125. unsigned char length[2];
  126. unsigned lelen, belen;
  127. size_t idlens[4];
  128. unsigned numidlens;
  129. unsigned current_idlen;
  130. ssize_t retval;
  131. size_t len;
  132. /* First two bytes are MSB,LSB of inclusive length. */
  133. retval = parport_read (port, length, 2);
  134. if (retval < 0)
  135. return retval;
  136. if (retval != 2)
  137. return -EIO;
  138. if (count < 2)
  139. return 0;
  140. memcpy(buffer, length, 2);
  141. len = 2;
  142. /* Some devices wrongly send LE length, and some send it two
  143. * bytes short. Construct a sorted array of lengths to try. */
  144. belen = (length[0] << 8) + length[1];
  145. lelen = (length[1] << 8) + length[0];
  146. idlens[0] = min(belen, lelen);
  147. idlens[1] = idlens[0]+2;
  148. if (belen != lelen) {
  149. int off = 2;
  150. /* Don't try lengths of 0x100 and 0x200 as 1 and 2 */
  151. if (idlens[0] <= 2)
  152. off = 0;
  153. idlens[off] = max(belen, lelen);
  154. idlens[off+1] = idlens[off]+2;
  155. numidlens = off+2;
  156. }
  157. else {
  158. /* Some devices don't truly implement Device ID, but
  159. * just return constant nibble forever. This catches
  160. * also those cases. */
  161. if (idlens[0] == 0 || idlens[0] > 0xFFF) {
  162. printk (KERN_DEBUG "%s: reported broken Device ID"
  163. " length of %#zX bytes\n",
  164. port->name, idlens[0]);
  165. return -EIO;
  166. }
  167. numidlens = 2;
  168. }
  169. /* Try to respect the given ID length despite all the bugs in
  170. * the ID length. Read according to shortest possible ID
  171. * first. */
  172. for (current_idlen = 0; current_idlen < numidlens; ++current_idlen) {
  173. size_t idlen = idlens[current_idlen];
  174. if (idlen+1 >= count)
  175. break;
  176. retval = parport_read (port, buffer+len, idlen-len);
  177. if (retval < 0)
  178. return retval;
  179. len += retval;
  180. if (port->physport->ieee1284.phase != IEEE1284_PH_HBUSY_DAVAIL) {
  181. if (belen != len) {
  182. printk (KERN_DEBUG "%s: Device ID was %zd bytes"
  183. " while device told it would be %d"
  184. " bytes\n",
  185. port->name, len, belen);
  186. }
  187. goto done;
  188. }
  189. /* This might end reading the Device ID too
  190. * soon. Hopefully the needed fields were already in
  191. * the first 256 bytes or so that we must have read so
  192. * far. */
  193. if (buffer[len-1] == ';') {
  194. printk (KERN_DEBUG "%s: Device ID reading stopped"
  195. " before device told data not available. "
  196. "Current idlen %u of %u, len bytes %02X %02X\n",
  197. port->name, current_idlen, numidlens,
  198. length[0], length[1]);
  199. goto done;
  200. }
  201. }
  202. if (current_idlen < numidlens) {
  203. /* Buffer not large enough, read to end of buffer. */
  204. size_t idlen, len2;
  205. if (len+1 < count) {
  206. retval = parport_read (port, buffer+len, count-len-1);
  207. if (retval < 0)
  208. return retval;
  209. len += retval;
  210. }
  211. /* Read the whole ID since some devices would not
  212. * otherwise give back the Device ID from beginning
  213. * next time when asked. */
  214. idlen = idlens[current_idlen];
  215. len2 = len;
  216. while(len2 < idlen && retval > 0) {
  217. char tmp[4];
  218. retval = parport_read (port, tmp,
  219. min(sizeof tmp, idlen-len2));
  220. if (retval < 0)
  221. return retval;
  222. len2 += retval;
  223. }
  224. }
  225. /* In addition, there are broken devices out there that don't
  226. even finish off with a semi-colon. We do not need to care
  227. about those at this time. */
  228. done:
  229. buffer[len] = '\0';
  230. return len;
  231. }
  232. /* Get Std 1284 Device ID. */
  233. ssize_t parport_device_id (int devnum, char *buffer, size_t count)
  234. {
  235. ssize_t retval = -ENXIO;
  236. struct pardevice *dev = parport_open (devnum, "Device ID probe");
  237. if (!dev)
  238. return -ENXIO;
  239. parport_claim_or_block (dev);
  240. /* Negotiate to compatibility mode, and then to device ID
  241. * mode. (This so that we start form beginning of device ID if
  242. * already in device ID mode.) */
  243. parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
  244. retval = parport_negotiate (dev->port,
  245. IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID);
  246. if (!retval) {
  247. retval = parport_read_device_id (dev->port, buffer, count);
  248. parport_negotiate (dev->port, IEEE1284_MODE_COMPAT);
  249. if (retval > 2)
  250. parse_data (dev->port, dev->daisy, buffer+2);
  251. }
  252. parport_release (dev);
  253. parport_close (dev);
  254. return retval;
  255. }