plip.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /* $Id: plip.c,v 1.1.1.1 2007/06/12 07:27:10 eyryu Exp $ */
  2. /* PLIP: A parallel port "network" driver for Linux. */
  3. /* This driver is for parallel port with 5-bit cable (LapLink (R) cable). */
  4. /*
  5. * Authors: Donald Becker <becker@scyld.com>
  6. * Tommy Thorn <thorn@daimi.aau.dk>
  7. * Tanabe Hiroyasu <hiro@sanpo.t.u-tokyo.ac.jp>
  8. * Alan Cox <gw4pts@gw4pts.ampr.org>
  9. * Peter Bauer <100136.3530@compuserve.com>
  10. * Niibe Yutaka <gniibe@mri.co.jp>
  11. * Nimrod Zimerman <zimerman@mailandnews.com>
  12. *
  13. * Enhancements:
  14. * Modularization and ifreq/ifmap support by Alan Cox.
  15. * Rewritten by Niibe Yutaka.
  16. * parport-sharing awareness code by Philip Blundell.
  17. * SMP locking by Niibe Yutaka.
  18. * Support for parallel ports with no IRQ (poll mode),
  19. * Modifications to use the parallel port API
  20. * by Nimrod Zimerman.
  21. *
  22. * Fixes:
  23. * Niibe Yutaka
  24. * - Module initialization.
  25. * - MTU fix.
  26. * - Make sure other end is OK, before sending a packet.
  27. * - Fix immediate timer problem.
  28. *
  29. * Al Viro
  30. * - Changed {enable,disable}_irq handling to make it work
  31. * with new ("stack") semantics.
  32. *
  33. * This program is free software; you can redistribute it and/or
  34. * modify it under the terms of the GNU General Public License
  35. * as published by the Free Software Foundation; either version
  36. * 2 of the License, or (at your option) any later version.
  37. */
  38. /*
  39. * Original version and the name 'PLIP' from Donald Becker <becker@scyld.com>
  40. * inspired by Russ Nelson's parallel port packet driver.
  41. *
  42. * NOTE:
  43. * Tanabe Hiroyasu had changed the protocol, and it was in Linux v1.0.
  44. * Because of the necessity to communicate to DOS machines with the
  45. * Crynwr packet driver, Peter Bauer changed the protocol again
  46. * back to original protocol.
  47. *
  48. * This version follows original PLIP protocol.
  49. * So, this PLIP can't communicate the PLIP of Linux v1.0.
  50. */
  51. /*
  52. * To use with DOS box, please do (Turn on ARP switch):
  53. * # ifconfig plip[0-2] arp
  54. */
  55. static const char version[] = "NET3 PLIP version 2.4-parport gniibe@mri.co.jp\n";
  56. /*
  57. Sources:
  58. Ideas and protocols came from Russ Nelson's <nelson@crynwr.com>
  59. "parallel.asm" parallel port packet driver.
  60. The "Crynwr" parallel port standard specifies the following protocol:
  61. Trigger by sending nibble '0x8' (this causes interrupt on other end)
  62. count-low octet
  63. count-high octet
  64. ... data octets
  65. checksum octet
  66. Each octet is sent as <wait for rx. '0x1?'> <send 0x10+(octet&0x0F)>
  67. <wait for rx. '0x0?'> <send 0x00+((octet>>4)&0x0F)>
  68. The packet is encapsulated as if it were ethernet.
  69. The cable used is a de facto standard parallel null cable -- sold as
  70. a "LapLink" cable by various places. You'll need a 12-conductor cable to
  71. make one yourself. The wiring is:
  72. SLCTIN 17 - 17
  73. GROUND 25 - 25
  74. D0->ERROR 2 - 15 15 - 2
  75. D1->SLCT 3 - 13 13 - 3
  76. D2->PAPOUT 4 - 12 12 - 4
  77. D3->ACK 5 - 10 10 - 5
  78. D4->BUSY 6 - 11 11 - 6
  79. Do not connect the other pins. They are
  80. D5,D6,D7 are 7,8,9
  81. STROBE is 1, FEED is 14, INIT is 16
  82. extra grounds are 18,19,20,21,22,23,24
  83. */
  84. #include <linux/module.h>
  85. #include <linux/kernel.h>
  86. #include <linux/types.h>
  87. #include <linux/fcntl.h>
  88. #include <linux/interrupt.h>
  89. #include <linux/string.h>
  90. #include <linux/if_ether.h>
  91. #include <linux/in.h>
  92. #include <linux/errno.h>
  93. #include <linux/delay.h>
  94. #include <linux/init.h>
  95. #include <linux/netdevice.h>
  96. #include <linux/etherdevice.h>
  97. #include <linux/inetdevice.h>
  98. #include <linux/skbuff.h>
  99. #include <linux/if_plip.h>
  100. #include <linux/workqueue.h>
  101. #include <linux/spinlock.h>
  102. #include <linux/parport.h>
  103. #include <linux/bitops.h>
  104. #include <net/neighbour.h>
  105. #include <asm/system.h>
  106. #include <asm/irq.h>
  107. #include <asm/byteorder.h>
  108. #include <asm/semaphore.h>
  109. /* Maximum number of devices to support. */
  110. #define PLIP_MAX 8
  111. /* Use 0 for production, 1 for verification, >2 for debug */
  112. #ifndef NET_DEBUG
  113. #define NET_DEBUG 1
  114. #endif
  115. static const unsigned int net_debug = NET_DEBUG;
  116. #define ENABLE(irq) if (irq != -1) enable_irq(irq)
  117. #define DISABLE(irq) if (irq != -1) disable_irq(irq)
  118. /* In micro second */
  119. #define PLIP_DELAY_UNIT 1
  120. /* Connection time out = PLIP_TRIGGER_WAIT * PLIP_DELAY_UNIT usec */
  121. #define PLIP_TRIGGER_WAIT 500
  122. /* Nibble time out = PLIP_NIBBLE_WAIT * PLIP_DELAY_UNIT usec */
  123. #define PLIP_NIBBLE_WAIT 3000
  124. /* Bottom halves */
  125. static void plip_kick_bh(struct work_struct *work);
  126. static void plip_bh(struct work_struct *work);
  127. static void plip_timer_bh(struct work_struct *work);
  128. /* Interrupt handler */
  129. static void plip_interrupt(int irq, void *dev_id);
  130. /* Functions for DEV methods */
  131. static int plip_tx_packet(struct sk_buff *skb, struct net_device *dev);
  132. static int plip_hard_header(struct sk_buff *skb, struct net_device *dev,
  133. unsigned short type, void *daddr,
  134. void *saddr, unsigned len);
  135. static int plip_hard_header_cache(struct neighbour *neigh,
  136. struct hh_cache *hh);
  137. static int plip_open(struct net_device *dev);
  138. static int plip_close(struct net_device *dev);
  139. static struct net_device_stats *plip_get_stats(struct net_device *dev);
  140. static int plip_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  141. static int plip_preempt(void *handle);
  142. static void plip_wakeup(void *handle);
  143. enum plip_connection_state {
  144. PLIP_CN_NONE=0,
  145. PLIP_CN_RECEIVE,
  146. PLIP_CN_SEND,
  147. PLIP_CN_CLOSING,
  148. PLIP_CN_ERROR
  149. };
  150. enum plip_packet_state {
  151. PLIP_PK_DONE=0,
  152. PLIP_PK_TRIGGER,
  153. PLIP_PK_LENGTH_LSB,
  154. PLIP_PK_LENGTH_MSB,
  155. PLIP_PK_DATA,
  156. PLIP_PK_CHECKSUM
  157. };
  158. enum plip_nibble_state {
  159. PLIP_NB_BEGIN,
  160. PLIP_NB_1,
  161. PLIP_NB_2,
  162. };
  163. struct plip_local {
  164. enum plip_packet_state state;
  165. enum plip_nibble_state nibble;
  166. union {
  167. struct {
  168. #if defined(__LITTLE_ENDIAN)
  169. unsigned char lsb;
  170. unsigned char msb;
  171. #elif defined(__BIG_ENDIAN)
  172. unsigned char msb;
  173. unsigned char lsb;
  174. #else
  175. #error "Please fix the endianness defines in <asm/byteorder.h>"
  176. #endif
  177. } b;
  178. unsigned short h;
  179. } length;
  180. unsigned short byte;
  181. unsigned char checksum;
  182. unsigned char data;
  183. struct sk_buff *skb;
  184. };
  185. struct net_local {
  186. struct net_device_stats enet_stats;
  187. struct net_device *dev;
  188. struct work_struct immediate;
  189. struct delayed_work deferred;
  190. struct delayed_work timer;
  191. struct plip_local snd_data;
  192. struct plip_local rcv_data;
  193. struct pardevice *pardev;
  194. unsigned long trigger;
  195. unsigned long nibble;
  196. enum plip_connection_state connection;
  197. unsigned short timeout_count;
  198. int is_deferred;
  199. int port_owner;
  200. int should_relinquish;
  201. int (*orig_hard_header)(struct sk_buff *skb, struct net_device *dev,
  202. unsigned short type, void *daddr,
  203. void *saddr, unsigned len);
  204. int (*orig_hard_header_cache)(struct neighbour *neigh,
  205. struct hh_cache *hh);
  206. spinlock_t lock;
  207. atomic_t kill_timer;
  208. struct semaphore killed_timer_sem;
  209. };
  210. static inline void enable_parport_interrupts (struct net_device *dev)
  211. {
  212. if (dev->irq != -1)
  213. {
  214. struct parport *port =
  215. ((struct net_local *)dev->priv)->pardev->port;
  216. port->ops->enable_irq (port);
  217. }
  218. }
  219. static inline void disable_parport_interrupts (struct net_device *dev)
  220. {
  221. if (dev->irq != -1)
  222. {
  223. struct parport *port =
  224. ((struct net_local *)dev->priv)->pardev->port;
  225. port->ops->disable_irq (port);
  226. }
  227. }
  228. static inline void write_data (struct net_device *dev, unsigned char data)
  229. {
  230. struct parport *port =
  231. ((struct net_local *)dev->priv)->pardev->port;
  232. port->ops->write_data (port, data);
  233. }
  234. static inline unsigned char read_status (struct net_device *dev)
  235. {
  236. struct parport *port =
  237. ((struct net_local *)dev->priv)->pardev->port;
  238. return port->ops->read_status (port);
  239. }
  240. /* Entry point of PLIP driver.
  241. Probe the hardware, and register/initialize the driver.
  242. PLIP is rather weird, because of the way it interacts with the parport
  243. system. It is _not_ initialised from Space.c. Instead, plip_init()
  244. is called, and that function makes up a "struct net_device" for each port, and
  245. then calls us here.
  246. */
  247. static void
  248. plip_init_netdev(struct net_device *dev)
  249. {
  250. struct net_local *nl = netdev_priv(dev);
  251. /* Then, override parts of it */
  252. dev->hard_start_xmit = plip_tx_packet;
  253. dev->open = plip_open;
  254. dev->stop = plip_close;
  255. dev->get_stats = plip_get_stats;
  256. dev->do_ioctl = plip_ioctl;
  257. dev->header_cache_update = NULL;
  258. dev->tx_queue_len = 10;
  259. dev->flags = IFF_POINTOPOINT|IFF_NOARP;
  260. memset(dev->dev_addr, 0xfc, ETH_ALEN);
  261. /* Set the private structure */
  262. nl->orig_hard_header = dev->hard_header;
  263. dev->hard_header = plip_hard_header;
  264. nl->orig_hard_header_cache = dev->hard_header_cache;
  265. dev->hard_header_cache = plip_hard_header_cache;
  266. nl->port_owner = 0;
  267. /* Initialize constants */
  268. nl->trigger = PLIP_TRIGGER_WAIT;
  269. nl->nibble = PLIP_NIBBLE_WAIT;
  270. /* Initialize task queue structures */
  271. INIT_WORK(&nl->immediate, plip_bh);
  272. INIT_DELAYED_WORK(&nl->deferred, plip_kick_bh);
  273. if (dev->irq == -1)
  274. INIT_DELAYED_WORK(&nl->timer, plip_timer_bh);
  275. spin_lock_init(&nl->lock);
  276. }
  277. /* Bottom half handler for the delayed request.
  278. This routine is kicked by do_timer().
  279. Request `plip_bh' to be invoked. */
  280. static void
  281. plip_kick_bh(struct work_struct *work)
  282. {
  283. struct net_local *nl =
  284. container_of(work, struct net_local, deferred.work);
  285. if (nl->is_deferred)
  286. schedule_work(&nl->immediate);
  287. }
  288. /* Forward declarations of internal routines */
  289. static int plip_none(struct net_device *, struct net_local *,
  290. struct plip_local *, struct plip_local *);
  291. static int plip_receive_packet(struct net_device *, struct net_local *,
  292. struct plip_local *, struct plip_local *);
  293. static int plip_send_packet(struct net_device *, struct net_local *,
  294. struct plip_local *, struct plip_local *);
  295. static int plip_connection_close(struct net_device *, struct net_local *,
  296. struct plip_local *, struct plip_local *);
  297. static int plip_error(struct net_device *, struct net_local *,
  298. struct plip_local *, struct plip_local *);
  299. static int plip_bh_timeout_error(struct net_device *dev, struct net_local *nl,
  300. struct plip_local *snd,
  301. struct plip_local *rcv,
  302. int error);
  303. #define OK 0
  304. #define TIMEOUT 1
  305. #define ERROR 2
  306. #define HS_TIMEOUT 3
  307. typedef int (*plip_func)(struct net_device *dev, struct net_local *nl,
  308. struct plip_local *snd, struct plip_local *rcv);
  309. static const plip_func connection_state_table[] =
  310. {
  311. plip_none,
  312. plip_receive_packet,
  313. plip_send_packet,
  314. plip_connection_close,
  315. plip_error
  316. };
  317. /* Bottom half handler of PLIP. */
  318. static void
  319. plip_bh(struct work_struct *work)
  320. {
  321. struct net_local *nl = container_of(work, struct net_local, immediate);
  322. struct plip_local *snd = &nl->snd_data;
  323. struct plip_local *rcv = &nl->rcv_data;
  324. plip_func f;
  325. int r;
  326. nl->is_deferred = 0;
  327. f = connection_state_table[nl->connection];
  328. if ((r = (*f)(nl->dev, nl, snd, rcv)) != OK
  329. && (r = plip_bh_timeout_error(nl->dev, nl, snd, rcv, r)) != OK) {
  330. nl->is_deferred = 1;
  331. schedule_delayed_work(&nl->deferred, 1);
  332. }
  333. }
  334. static void
  335. plip_timer_bh(struct work_struct *work)
  336. {
  337. struct net_local *nl =
  338. container_of(work, struct net_local, timer.work);
  339. if (!(atomic_read (&nl->kill_timer))) {
  340. plip_interrupt (-1, nl->dev);
  341. schedule_delayed_work(&nl->timer, 1);
  342. }
  343. else {
  344. up (&nl->killed_timer_sem);
  345. }
  346. }
  347. static int
  348. plip_bh_timeout_error(struct net_device *dev, struct net_local *nl,
  349. struct plip_local *snd, struct plip_local *rcv,
  350. int error)
  351. {
  352. unsigned char c0;
  353. /*
  354. * This is tricky. If we got here from the beginning of send (either
  355. * with ERROR or HS_TIMEOUT) we have IRQ enabled. Otherwise it's
  356. * already disabled. With the old variant of {enable,disable}_irq()
  357. * extra disable_irq() was a no-op. Now it became mortal - it's
  358. * unbalanced and thus we'll never re-enable IRQ (until rmmod plip,
  359. * that is). So we have to treat HS_TIMEOUT and ERROR from send
  360. * in a special way.
  361. */
  362. spin_lock_irq(&nl->lock);
  363. if (nl->connection == PLIP_CN_SEND) {
  364. if (error != ERROR) { /* Timeout */
  365. nl->timeout_count++;
  366. if ((error == HS_TIMEOUT
  367. && nl->timeout_count <= 10)
  368. || nl->timeout_count <= 3) {
  369. spin_unlock_irq(&nl->lock);
  370. /* Try again later */
  371. return TIMEOUT;
  372. }
  373. c0 = read_status(dev);
  374. printk(KERN_WARNING "%s: transmit timeout(%d,%02x)\n",
  375. dev->name, snd->state, c0);
  376. } else
  377. error = HS_TIMEOUT;
  378. nl->enet_stats.tx_errors++;
  379. nl->enet_stats.tx_aborted_errors++;
  380. } else if (nl->connection == PLIP_CN_RECEIVE) {
  381. if (rcv->state == PLIP_PK_TRIGGER) {
  382. /* Transmission was interrupted. */
  383. spin_unlock_irq(&nl->lock);
  384. return OK;
  385. }
  386. if (error != ERROR) { /* Timeout */
  387. if (++nl->timeout_count <= 3) {
  388. spin_unlock_irq(&nl->lock);
  389. /* Try again later */
  390. return TIMEOUT;
  391. }
  392. c0 = read_status(dev);
  393. printk(KERN_WARNING "%s: receive timeout(%d,%02x)\n",
  394. dev->name, rcv->state, c0);
  395. }
  396. nl->enet_stats.rx_dropped++;
  397. }
  398. rcv->state = PLIP_PK_DONE;
  399. if (rcv->skb) {
  400. kfree_skb(rcv->skb);
  401. rcv->skb = NULL;
  402. }
  403. snd->state = PLIP_PK_DONE;
  404. if (snd->skb) {
  405. dev_kfree_skb(snd->skb);
  406. snd->skb = NULL;
  407. }
  408. spin_unlock_irq(&nl->lock);
  409. if (error == HS_TIMEOUT) {
  410. DISABLE(dev->irq);
  411. synchronize_irq(dev->irq);
  412. }
  413. disable_parport_interrupts (dev);
  414. netif_stop_queue (dev);
  415. nl->connection = PLIP_CN_ERROR;
  416. write_data (dev, 0x00);
  417. return TIMEOUT;
  418. }
  419. static int
  420. plip_none(struct net_device *dev, struct net_local *nl,
  421. struct plip_local *snd, struct plip_local *rcv)
  422. {
  423. return OK;
  424. }
  425. /* PLIP_RECEIVE --- receive a byte(two nibbles)
  426. Returns OK on success, TIMEOUT on timeout */
  427. static inline int
  428. plip_receive(unsigned short nibble_timeout, struct net_device *dev,
  429. enum plip_nibble_state *ns_p, unsigned char *data_p)
  430. {
  431. unsigned char c0, c1;
  432. unsigned int cx;
  433. switch (*ns_p) {
  434. case PLIP_NB_BEGIN:
  435. cx = nibble_timeout;
  436. while (1) {
  437. c0 = read_status(dev);
  438. udelay(PLIP_DELAY_UNIT);
  439. if ((c0 & 0x80) == 0) {
  440. c1 = read_status(dev);
  441. if (c0 == c1)
  442. break;
  443. }
  444. if (--cx == 0)
  445. return TIMEOUT;
  446. }
  447. *data_p = (c0 >> 3) & 0x0f;
  448. write_data (dev, 0x10); /* send ACK */
  449. *ns_p = PLIP_NB_1;
  450. case PLIP_NB_1:
  451. cx = nibble_timeout;
  452. while (1) {
  453. c0 = read_status(dev);
  454. udelay(PLIP_DELAY_UNIT);
  455. if (c0 & 0x80) {
  456. c1 = read_status(dev);
  457. if (c0 == c1)
  458. break;
  459. }
  460. if (--cx == 0)
  461. return TIMEOUT;
  462. }
  463. *data_p |= (c0 << 1) & 0xf0;
  464. write_data (dev, 0x00); /* send ACK */
  465. *ns_p = PLIP_NB_BEGIN;
  466. case PLIP_NB_2:
  467. break;
  468. }
  469. return OK;
  470. }
  471. /*
  472. * Determine the packet's protocol ID. The rule here is that we
  473. * assume 802.3 if the type field is short enough to be a length.
  474. * This is normal practice and works for any 'now in use' protocol.
  475. *
  476. * PLIP is ethernet ish but the daddr might not be valid if unicast.
  477. * PLIP fortunately has no bus architecture (its Point-to-point).
  478. *
  479. * We can't fix the daddr thing as that quirk (more bug) is embedded
  480. * in far too many old systems not all even running Linux.
  481. */
  482. static __be16 plip_type_trans(struct sk_buff *skb, struct net_device *dev)
  483. {
  484. struct ethhdr *eth;
  485. unsigned char *rawp;
  486. skb->mac.raw=skb->data;
  487. skb_pull(skb,dev->hard_header_len);
  488. eth = eth_hdr(skb);
  489. if(*eth->h_dest&1)
  490. {
  491. if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
  492. skb->pkt_type=PACKET_BROADCAST;
  493. else
  494. skb->pkt_type=PACKET_MULTICAST;
  495. }
  496. /*
  497. * This ALLMULTI check should be redundant by 1.4
  498. * so don't forget to remove it.
  499. */
  500. if (ntohs(eth->h_proto) >= 1536)
  501. return eth->h_proto;
  502. rawp = skb->data;
  503. /*
  504. * This is a magic hack to spot IPX packets. Older Novell breaks
  505. * the protocol design and runs IPX over 802.3 without an 802.2 LLC
  506. * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
  507. * won't work for fault tolerant netware but does for the rest.
  508. */
  509. if (*(unsigned short *)rawp == 0xFFFF)
  510. return htons(ETH_P_802_3);
  511. /*
  512. * Real 802.2 LLC
  513. */
  514. return htons(ETH_P_802_2);
  515. }
  516. /* PLIP_RECEIVE_PACKET --- receive a packet */
  517. static int
  518. plip_receive_packet(struct net_device *dev, struct net_local *nl,
  519. struct plip_local *snd, struct plip_local *rcv)
  520. {
  521. unsigned short nibble_timeout = nl->nibble;
  522. unsigned char *lbuf;
  523. switch (rcv->state) {
  524. case PLIP_PK_TRIGGER:
  525. DISABLE(dev->irq);
  526. /* Don't need to synchronize irq, as we can safely ignore it */
  527. disable_parport_interrupts (dev);
  528. write_data (dev, 0x01); /* send ACK */
  529. if (net_debug > 2)
  530. printk(KERN_DEBUG "%s: receive start\n", dev->name);
  531. rcv->state = PLIP_PK_LENGTH_LSB;
  532. rcv->nibble = PLIP_NB_BEGIN;
  533. case PLIP_PK_LENGTH_LSB:
  534. if (snd->state != PLIP_PK_DONE) {
  535. if (plip_receive(nl->trigger, dev,
  536. &rcv->nibble, &rcv->length.b.lsb)) {
  537. /* collision, here dev->tbusy == 1 */
  538. rcv->state = PLIP_PK_DONE;
  539. nl->is_deferred = 1;
  540. nl->connection = PLIP_CN_SEND;
  541. schedule_delayed_work(&nl->deferred, 1);
  542. enable_parport_interrupts (dev);
  543. ENABLE(dev->irq);
  544. return OK;
  545. }
  546. } else {
  547. if (plip_receive(nibble_timeout, dev,
  548. &rcv->nibble, &rcv->length.b.lsb))
  549. return TIMEOUT;
  550. }
  551. rcv->state = PLIP_PK_LENGTH_MSB;
  552. case PLIP_PK_LENGTH_MSB:
  553. if (plip_receive(nibble_timeout, dev,
  554. &rcv->nibble, &rcv->length.b.msb))
  555. return TIMEOUT;
  556. if (rcv->length.h > dev->mtu + dev->hard_header_len
  557. || rcv->length.h < 8) {
  558. printk(KERN_WARNING "%s: bogus packet size %d.\n", dev->name, rcv->length.h);
  559. return ERROR;
  560. }
  561. /* Malloc up new buffer. */
  562. rcv->skb = dev_alloc_skb(rcv->length.h + 2);
  563. if (rcv->skb == NULL) {
  564. printk(KERN_ERR "%s: Memory squeeze.\n", dev->name);
  565. return ERROR;
  566. }
  567. skb_reserve(rcv->skb, 2); /* Align IP on 16 byte boundaries */
  568. skb_put(rcv->skb,rcv->length.h);
  569. rcv->skb->dev = dev;
  570. rcv->state = PLIP_PK_DATA;
  571. rcv->byte = 0;
  572. rcv->checksum = 0;
  573. case PLIP_PK_DATA:
  574. lbuf = rcv->skb->data;
  575. do
  576. if (plip_receive(nibble_timeout, dev,
  577. &rcv->nibble, &lbuf[rcv->byte]))
  578. return TIMEOUT;
  579. while (++rcv->byte < rcv->length.h);
  580. do
  581. rcv->checksum += lbuf[--rcv->byte];
  582. while (rcv->byte);
  583. rcv->state = PLIP_PK_CHECKSUM;
  584. case PLIP_PK_CHECKSUM:
  585. if (plip_receive(nibble_timeout, dev,
  586. &rcv->nibble, &rcv->data))
  587. return TIMEOUT;
  588. if (rcv->data != rcv->checksum) {
  589. nl->enet_stats.rx_crc_errors++;
  590. if (net_debug)
  591. printk(KERN_DEBUG "%s: checksum error\n", dev->name);
  592. return ERROR;
  593. }
  594. rcv->state = PLIP_PK_DONE;
  595. case PLIP_PK_DONE:
  596. /* Inform the upper layer for the arrival of a packet. */
  597. rcv->skb->protocol=plip_type_trans(rcv->skb, dev);
  598. netif_rx(rcv->skb);
  599. dev->last_rx = jiffies;
  600. nl->enet_stats.rx_bytes += rcv->length.h;
  601. nl->enet_stats.rx_packets++;
  602. rcv->skb = NULL;
  603. if (net_debug > 2)
  604. printk(KERN_DEBUG "%s: receive end\n", dev->name);
  605. /* Close the connection. */
  606. write_data (dev, 0x00);
  607. spin_lock_irq(&nl->lock);
  608. if (snd->state != PLIP_PK_DONE) {
  609. nl->connection = PLIP_CN_SEND;
  610. spin_unlock_irq(&nl->lock);
  611. schedule_work(&nl->immediate);
  612. enable_parport_interrupts (dev);
  613. ENABLE(dev->irq);
  614. return OK;
  615. } else {
  616. nl->connection = PLIP_CN_NONE;
  617. spin_unlock_irq(&nl->lock);
  618. enable_parport_interrupts (dev);
  619. ENABLE(dev->irq);
  620. return OK;
  621. }
  622. }
  623. return OK;
  624. }
  625. /* PLIP_SEND --- send a byte (two nibbles)
  626. Returns OK on success, TIMEOUT when timeout */
  627. static inline int
  628. plip_send(unsigned short nibble_timeout, struct net_device *dev,
  629. enum plip_nibble_state *ns_p, unsigned char data)
  630. {
  631. unsigned char c0;
  632. unsigned int cx;
  633. switch (*ns_p) {
  634. case PLIP_NB_BEGIN:
  635. write_data (dev, data & 0x0f);
  636. *ns_p = PLIP_NB_1;
  637. case PLIP_NB_1:
  638. write_data (dev, 0x10 | (data & 0x0f));
  639. cx = nibble_timeout;
  640. while (1) {
  641. c0 = read_status(dev);
  642. if ((c0 & 0x80) == 0)
  643. break;
  644. if (--cx == 0)
  645. return TIMEOUT;
  646. udelay(PLIP_DELAY_UNIT);
  647. }
  648. write_data (dev, 0x10 | (data >> 4));
  649. *ns_p = PLIP_NB_2;
  650. case PLIP_NB_2:
  651. write_data (dev, (data >> 4));
  652. cx = nibble_timeout;
  653. while (1) {
  654. c0 = read_status(dev);
  655. if (c0 & 0x80)
  656. break;
  657. if (--cx == 0)
  658. return TIMEOUT;
  659. udelay(PLIP_DELAY_UNIT);
  660. }
  661. *ns_p = PLIP_NB_BEGIN;
  662. return OK;
  663. }
  664. return OK;
  665. }
  666. /* PLIP_SEND_PACKET --- send a packet */
  667. static int
  668. plip_send_packet(struct net_device *dev, struct net_local *nl,
  669. struct plip_local *snd, struct plip_local *rcv)
  670. {
  671. unsigned short nibble_timeout = nl->nibble;
  672. unsigned char *lbuf;
  673. unsigned char c0;
  674. unsigned int cx;
  675. if (snd->skb == NULL || (lbuf = snd->skb->data) == NULL) {
  676. printk(KERN_DEBUG "%s: send skb lost\n", dev->name);
  677. snd->state = PLIP_PK_DONE;
  678. snd->skb = NULL;
  679. return ERROR;
  680. }
  681. switch (snd->state) {
  682. case PLIP_PK_TRIGGER:
  683. if ((read_status(dev) & 0xf8) != 0x80)
  684. return HS_TIMEOUT;
  685. /* Trigger remote rx interrupt. */
  686. write_data (dev, 0x08);
  687. cx = nl->trigger;
  688. while (1) {
  689. udelay(PLIP_DELAY_UNIT);
  690. spin_lock_irq(&nl->lock);
  691. if (nl->connection == PLIP_CN_RECEIVE) {
  692. spin_unlock_irq(&nl->lock);
  693. /* Interrupted. */
  694. nl->enet_stats.collisions++;
  695. return OK;
  696. }
  697. c0 = read_status(dev);
  698. if (c0 & 0x08) {
  699. spin_unlock_irq(&nl->lock);
  700. DISABLE(dev->irq);
  701. synchronize_irq(dev->irq);
  702. if (nl->connection == PLIP_CN_RECEIVE) {
  703. /* Interrupted.
  704. We don't need to enable irq,
  705. as it is soon disabled. */
  706. /* Yes, we do. New variant of
  707. {enable,disable}_irq *counts*
  708. them. -- AV */
  709. ENABLE(dev->irq);
  710. nl->enet_stats.collisions++;
  711. return OK;
  712. }
  713. disable_parport_interrupts (dev);
  714. if (net_debug > 2)
  715. printk(KERN_DEBUG "%s: send start\n", dev->name);
  716. snd->state = PLIP_PK_LENGTH_LSB;
  717. snd->nibble = PLIP_NB_BEGIN;
  718. nl->timeout_count = 0;
  719. break;
  720. }
  721. spin_unlock_irq(&nl->lock);
  722. if (--cx == 0) {
  723. write_data (dev, 0x00);
  724. return HS_TIMEOUT;
  725. }
  726. }
  727. case PLIP_PK_LENGTH_LSB:
  728. if (plip_send(nibble_timeout, dev,
  729. &snd->nibble, snd->length.b.lsb))
  730. return TIMEOUT;
  731. snd->state = PLIP_PK_LENGTH_MSB;
  732. case PLIP_PK_LENGTH_MSB:
  733. if (plip_send(nibble_timeout, dev,
  734. &snd->nibble, snd->length.b.msb))
  735. return TIMEOUT;
  736. snd->state = PLIP_PK_DATA;
  737. snd->byte = 0;
  738. snd->checksum = 0;
  739. case PLIP_PK_DATA:
  740. do
  741. if (plip_send(nibble_timeout, dev,
  742. &snd->nibble, lbuf[snd->byte]))
  743. return TIMEOUT;
  744. while (++snd->byte < snd->length.h);
  745. do
  746. snd->checksum += lbuf[--snd->byte];
  747. while (snd->byte);
  748. snd->state = PLIP_PK_CHECKSUM;
  749. case PLIP_PK_CHECKSUM:
  750. if (plip_send(nibble_timeout, dev,
  751. &snd->nibble, snd->checksum))
  752. return TIMEOUT;
  753. nl->enet_stats.tx_bytes += snd->skb->len;
  754. dev_kfree_skb(snd->skb);
  755. nl->enet_stats.tx_packets++;
  756. snd->state = PLIP_PK_DONE;
  757. case PLIP_PK_DONE:
  758. /* Close the connection */
  759. write_data (dev, 0x00);
  760. snd->skb = NULL;
  761. if (net_debug > 2)
  762. printk(KERN_DEBUG "%s: send end\n", dev->name);
  763. nl->connection = PLIP_CN_CLOSING;
  764. nl->is_deferred = 1;
  765. schedule_delayed_work(&nl->deferred, 1);
  766. enable_parport_interrupts (dev);
  767. ENABLE(dev->irq);
  768. return OK;
  769. }
  770. return OK;
  771. }
  772. static int
  773. plip_connection_close(struct net_device *dev, struct net_local *nl,
  774. struct plip_local *snd, struct plip_local *rcv)
  775. {
  776. spin_lock_irq(&nl->lock);
  777. if (nl->connection == PLIP_CN_CLOSING) {
  778. nl->connection = PLIP_CN_NONE;
  779. netif_wake_queue (dev);
  780. }
  781. spin_unlock_irq(&nl->lock);
  782. if (nl->should_relinquish) {
  783. nl->should_relinquish = nl->port_owner = 0;
  784. parport_release(nl->pardev);
  785. }
  786. return OK;
  787. }
  788. /* PLIP_ERROR --- wait till other end settled */
  789. static int
  790. plip_error(struct net_device *dev, struct net_local *nl,
  791. struct plip_local *snd, struct plip_local *rcv)
  792. {
  793. unsigned char status;
  794. status = read_status(dev);
  795. if ((status & 0xf8) == 0x80) {
  796. if (net_debug > 2)
  797. printk(KERN_DEBUG "%s: reset interface.\n", dev->name);
  798. nl->connection = PLIP_CN_NONE;
  799. nl->should_relinquish = 0;
  800. netif_start_queue (dev);
  801. enable_parport_interrupts (dev);
  802. ENABLE(dev->irq);
  803. netif_wake_queue (dev);
  804. } else {
  805. nl->is_deferred = 1;
  806. schedule_delayed_work(&nl->deferred, 1);
  807. }
  808. return OK;
  809. }
  810. /* Handle the parallel port interrupts. */
  811. static void
  812. plip_interrupt(int irq, void *dev_id)
  813. {
  814. struct net_device *dev = dev_id;
  815. struct net_local *nl;
  816. struct plip_local *rcv;
  817. unsigned char c0;
  818. nl = netdev_priv(dev);
  819. rcv = &nl->rcv_data;
  820. spin_lock_irq (&nl->lock);
  821. c0 = read_status(dev);
  822. if ((c0 & 0xf8) != 0xc0) {
  823. if ((dev->irq != -1) && (net_debug > 1))
  824. printk(KERN_DEBUG "%s: spurious interrupt\n", dev->name);
  825. spin_unlock_irq (&nl->lock);
  826. return;
  827. }
  828. if (net_debug > 3)
  829. printk(KERN_DEBUG "%s: interrupt.\n", dev->name);
  830. switch (nl->connection) {
  831. case PLIP_CN_CLOSING:
  832. netif_wake_queue (dev);
  833. case PLIP_CN_NONE:
  834. case PLIP_CN_SEND:
  835. rcv->state = PLIP_PK_TRIGGER;
  836. nl->connection = PLIP_CN_RECEIVE;
  837. nl->timeout_count = 0;
  838. schedule_work(&nl->immediate);
  839. break;
  840. case PLIP_CN_RECEIVE:
  841. /* May occur because there is race condition
  842. around test and set of dev->interrupt.
  843. Ignore this interrupt. */
  844. break;
  845. case PLIP_CN_ERROR:
  846. printk(KERN_ERR "%s: receive interrupt in error state\n", dev->name);
  847. break;
  848. }
  849. spin_unlock_irq(&nl->lock);
  850. }
  851. static int
  852. plip_tx_packet(struct sk_buff *skb, struct net_device *dev)
  853. {
  854. struct net_local *nl = netdev_priv(dev);
  855. struct plip_local *snd = &nl->snd_data;
  856. if (netif_queue_stopped(dev))
  857. return 1;
  858. /* We may need to grab the bus */
  859. if (!nl->port_owner) {
  860. if (parport_claim(nl->pardev))
  861. return 1;
  862. nl->port_owner = 1;
  863. }
  864. netif_stop_queue (dev);
  865. if (skb->len > dev->mtu + dev->hard_header_len) {
  866. printk(KERN_WARNING "%s: packet too big, %d.\n", dev->name, (int)skb->len);
  867. netif_start_queue (dev);
  868. return 1;
  869. }
  870. if (net_debug > 2)
  871. printk(KERN_DEBUG "%s: send request\n", dev->name);
  872. spin_lock_irq(&nl->lock);
  873. dev->trans_start = jiffies;
  874. snd->skb = skb;
  875. snd->length.h = skb->len;
  876. snd->state = PLIP_PK_TRIGGER;
  877. if (nl->connection == PLIP_CN_NONE) {
  878. nl->connection = PLIP_CN_SEND;
  879. nl->timeout_count = 0;
  880. }
  881. schedule_work(&nl->immediate);
  882. spin_unlock_irq(&nl->lock);
  883. return 0;
  884. }
  885. static void
  886. plip_rewrite_address(struct net_device *dev, struct ethhdr *eth)
  887. {
  888. struct in_device *in_dev;
  889. if ((in_dev=dev->ip_ptr) != NULL) {
  890. /* Any address will do - we take the first */
  891. struct in_ifaddr *ifa=in_dev->ifa_list;
  892. if (ifa != NULL) {
  893. memcpy(eth->h_source, dev->dev_addr, 6);
  894. memset(eth->h_dest, 0xfc, 2);
  895. memcpy(eth->h_dest+2, &ifa->ifa_address, 4);
  896. }
  897. }
  898. }
  899. static int
  900. plip_hard_header(struct sk_buff *skb, struct net_device *dev,
  901. unsigned short type, void *daddr,
  902. void *saddr, unsigned len)
  903. {
  904. struct net_local *nl = netdev_priv(dev);
  905. int ret;
  906. if ((ret = nl->orig_hard_header(skb, dev, type, daddr, saddr, len)) >= 0)
  907. plip_rewrite_address (dev, (struct ethhdr *)skb->data);
  908. return ret;
  909. }
  910. int plip_hard_header_cache(struct neighbour *neigh,
  911. struct hh_cache *hh)
  912. {
  913. struct net_local *nl = neigh->dev->priv;
  914. int ret;
  915. if ((ret = nl->orig_hard_header_cache(neigh, hh)) == 0)
  916. {
  917. struct ethhdr *eth;
  918. eth = (struct ethhdr*)(((u8*)hh->hh_data) +
  919. HH_DATA_OFF(sizeof(*eth)));
  920. plip_rewrite_address (neigh->dev, eth);
  921. }
  922. return ret;
  923. }
  924. /* Open/initialize the board. This is called (in the current kernel)
  925. sometime after booting when the 'ifconfig' program is run.
  926. This routine gets exclusive access to the parallel port by allocating
  927. its IRQ line.
  928. */
  929. static int
  930. plip_open(struct net_device *dev)
  931. {
  932. struct net_local *nl = netdev_priv(dev);
  933. struct in_device *in_dev;
  934. /* Grab the port */
  935. if (!nl->port_owner) {
  936. if (parport_claim(nl->pardev)) return -EAGAIN;
  937. nl->port_owner = 1;
  938. }
  939. nl->should_relinquish = 0;
  940. /* Clear the data port. */
  941. write_data (dev, 0x00);
  942. /* Enable rx interrupt. */
  943. enable_parport_interrupts (dev);
  944. if (dev->irq == -1)
  945. {
  946. atomic_set (&nl->kill_timer, 0);
  947. schedule_delayed_work(&nl->timer, 1);
  948. }
  949. /* Initialize the state machine. */
  950. nl->rcv_data.state = nl->snd_data.state = PLIP_PK_DONE;
  951. nl->rcv_data.skb = nl->snd_data.skb = NULL;
  952. nl->connection = PLIP_CN_NONE;
  953. nl->is_deferred = 0;
  954. /* Fill in the MAC-level header.
  955. We used to abuse dev->broadcast to store the point-to-point
  956. MAC address, but we no longer do it. Instead, we fetch the
  957. interface address whenever it is needed, which is cheap enough
  958. because we use the hh_cache. Actually, abusing dev->broadcast
  959. didn't work, because when using plip_open the point-to-point
  960. address isn't yet known.
  961. PLIP doesn't have a real MAC address, but we need it to be
  962. DOS compatible, and to properly support taps (otherwise,
  963. when the device address isn't identical to the address of a
  964. received frame, the kernel incorrectly drops it). */
  965. if ((in_dev=dev->ip_ptr) != NULL) {
  966. /* Any address will do - we take the first. We already
  967. have the first two bytes filled with 0xfc, from
  968. plip_init_dev(). */
  969. struct in_ifaddr *ifa=in_dev->ifa_list;
  970. if (ifa != NULL) {
  971. memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
  972. }
  973. }
  974. netif_start_queue (dev);
  975. return 0;
  976. }
  977. /* The inverse routine to plip_open (). */
  978. static int
  979. plip_close(struct net_device *dev)
  980. {
  981. struct net_local *nl = netdev_priv(dev);
  982. struct plip_local *snd = &nl->snd_data;
  983. struct plip_local *rcv = &nl->rcv_data;
  984. netif_stop_queue (dev);
  985. DISABLE(dev->irq);
  986. synchronize_irq(dev->irq);
  987. if (dev->irq == -1)
  988. {
  989. init_MUTEX_LOCKED (&nl->killed_timer_sem);
  990. atomic_set (&nl->kill_timer, 1);
  991. down (&nl->killed_timer_sem);
  992. }
  993. #ifdef NOTDEF
  994. outb(0x00, PAR_DATA(dev));
  995. #endif
  996. nl->is_deferred = 0;
  997. nl->connection = PLIP_CN_NONE;
  998. if (nl->port_owner) {
  999. parport_release(nl->pardev);
  1000. nl->port_owner = 0;
  1001. }
  1002. snd->state = PLIP_PK_DONE;
  1003. if (snd->skb) {
  1004. dev_kfree_skb(snd->skb);
  1005. snd->skb = NULL;
  1006. }
  1007. rcv->state = PLIP_PK_DONE;
  1008. if (rcv->skb) {
  1009. kfree_skb(rcv->skb);
  1010. rcv->skb = NULL;
  1011. }
  1012. #ifdef NOTDEF
  1013. /* Reset. */
  1014. outb(0x00, PAR_CONTROL(dev));
  1015. #endif
  1016. return 0;
  1017. }
  1018. static int
  1019. plip_preempt(void *handle)
  1020. {
  1021. struct net_device *dev = (struct net_device *)handle;
  1022. struct net_local *nl = netdev_priv(dev);
  1023. /* Stand our ground if a datagram is on the wire */
  1024. if (nl->connection != PLIP_CN_NONE) {
  1025. nl->should_relinquish = 1;
  1026. return 1;
  1027. }
  1028. nl->port_owner = 0; /* Remember that we released the bus */
  1029. return 0;
  1030. }
  1031. static void
  1032. plip_wakeup(void *handle)
  1033. {
  1034. struct net_device *dev = (struct net_device *)handle;
  1035. struct net_local *nl = netdev_priv(dev);
  1036. if (nl->port_owner) {
  1037. /* Why are we being woken up? */
  1038. printk(KERN_DEBUG "%s: why am I being woken up?\n", dev->name);
  1039. if (!parport_claim(nl->pardev))
  1040. /* bus_owner is already set (but why?) */
  1041. printk(KERN_DEBUG "%s: I'm broken.\n", dev->name);
  1042. else
  1043. return;
  1044. }
  1045. if (!(dev->flags & IFF_UP))
  1046. /* Don't need the port when the interface is down */
  1047. return;
  1048. if (!parport_claim(nl->pardev)) {
  1049. nl->port_owner = 1;
  1050. /* Clear the data port. */
  1051. write_data (dev, 0x00);
  1052. }
  1053. return;
  1054. }
  1055. static struct net_device_stats *
  1056. plip_get_stats(struct net_device *dev)
  1057. {
  1058. struct net_local *nl = netdev_priv(dev);
  1059. struct net_device_stats *r = &nl->enet_stats;
  1060. return r;
  1061. }
  1062. static int
  1063. plip_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1064. {
  1065. struct net_local *nl = netdev_priv(dev);
  1066. struct plipconf *pc = (struct plipconf *) &rq->ifr_ifru;
  1067. if (cmd != SIOCDEVPLIP)
  1068. return -EOPNOTSUPP;
  1069. switch(pc->pcmd) {
  1070. case PLIP_GET_TIMEOUT:
  1071. pc->trigger = nl->trigger;
  1072. pc->nibble = nl->nibble;
  1073. break;
  1074. case PLIP_SET_TIMEOUT:
  1075. if(!capable(CAP_NET_ADMIN))
  1076. return -EPERM;
  1077. nl->trigger = pc->trigger;
  1078. nl->nibble = pc->nibble;
  1079. break;
  1080. default:
  1081. return -EOPNOTSUPP;
  1082. }
  1083. return 0;
  1084. }
  1085. static int parport[PLIP_MAX] = { [0 ... PLIP_MAX-1] = -1 };
  1086. static int timid;
  1087. module_param_array(parport, int, NULL, 0);
  1088. module_param(timid, int, 0);
  1089. MODULE_PARM_DESC(parport, "List of parport device numbers to use by plip");
  1090. static struct net_device *dev_plip[PLIP_MAX] = { NULL, };
  1091. static inline int
  1092. plip_searchfor(int list[], int a)
  1093. {
  1094. int i;
  1095. for (i = 0; i < PLIP_MAX && list[i] != -1; i++) {
  1096. if (list[i] == a) return 1;
  1097. }
  1098. return 0;
  1099. }
  1100. /* plip_attach() is called (by the parport code) when a port is
  1101. * available to use. */
  1102. static void plip_attach (struct parport *port)
  1103. {
  1104. static int unit;
  1105. struct net_device *dev;
  1106. struct net_local *nl;
  1107. char name[IFNAMSIZ];
  1108. if ((parport[0] == -1 && (!timid || !port->devices)) ||
  1109. plip_searchfor(parport, port->number)) {
  1110. if (unit == PLIP_MAX) {
  1111. printk(KERN_ERR "plip: too many devices\n");
  1112. return;
  1113. }
  1114. sprintf(name, "plip%d", unit);
  1115. dev = alloc_etherdev(sizeof(struct net_local));
  1116. if (!dev) {
  1117. printk(KERN_ERR "plip: memory squeeze\n");
  1118. return;
  1119. }
  1120. strcpy(dev->name, name);
  1121. SET_MODULE_OWNER(dev);
  1122. dev->irq = port->irq;
  1123. dev->base_addr = port->base;
  1124. if (port->irq == -1) {
  1125. printk(KERN_INFO "plip: %s has no IRQ. Using IRQ-less mode,"
  1126. "which is fairly inefficient!\n", port->name);
  1127. }
  1128. nl = netdev_priv(dev);
  1129. nl->dev = dev;
  1130. nl->pardev = parport_register_device(port, name, plip_preempt,
  1131. plip_wakeup, plip_interrupt,
  1132. 0, dev);
  1133. if (!nl->pardev) {
  1134. printk(KERN_ERR "%s: parport_register failed\n", name);
  1135. goto err_free_dev;
  1136. return;
  1137. }
  1138. plip_init_netdev(dev);
  1139. if (register_netdev(dev)) {
  1140. printk(KERN_ERR "%s: network register failed\n", name);
  1141. goto err_parport_unregister;
  1142. }
  1143. printk(KERN_INFO "%s", version);
  1144. if (dev->irq != -1)
  1145. printk(KERN_INFO "%s: Parallel port at %#3lx, "
  1146. "using IRQ %d.\n",
  1147. dev->name, dev->base_addr, dev->irq);
  1148. else
  1149. printk(KERN_INFO "%s: Parallel port at %#3lx, "
  1150. "not using IRQ.\n",
  1151. dev->name, dev->base_addr);
  1152. dev_plip[unit++] = dev;
  1153. }
  1154. return;
  1155. err_parport_unregister:
  1156. parport_unregister_device(nl->pardev);
  1157. err_free_dev:
  1158. free_netdev(dev);
  1159. return;
  1160. }
  1161. /* plip_detach() is called (by the parport code) when a port is
  1162. * no longer available to use. */
  1163. static void plip_detach (struct parport *port)
  1164. {
  1165. /* Nothing to do */
  1166. }
  1167. static struct parport_driver plip_driver = {
  1168. .name = "plip",
  1169. .attach = plip_attach,
  1170. .detach = plip_detach
  1171. };
  1172. static void __exit plip_cleanup_module (void)
  1173. {
  1174. struct net_device *dev;
  1175. int i;
  1176. parport_unregister_driver (&plip_driver);
  1177. for (i=0; i < PLIP_MAX; i++) {
  1178. if ((dev = dev_plip[i])) {
  1179. struct net_local *nl = netdev_priv(dev);
  1180. unregister_netdev(dev);
  1181. if (nl->port_owner)
  1182. parport_release(nl->pardev);
  1183. parport_unregister_device(nl->pardev);
  1184. free_netdev(dev);
  1185. dev_plip[i] = NULL;
  1186. }
  1187. }
  1188. }
  1189. #ifndef MODULE
  1190. static int parport_ptr;
  1191. static int __init plip_setup(char *str)
  1192. {
  1193. int ints[4];
  1194. str = get_options(str, ARRAY_SIZE(ints), ints);
  1195. /* Ugh. */
  1196. if (!strncmp(str, "parport", 7)) {
  1197. int n = simple_strtoul(str+7, NULL, 10);
  1198. if (parport_ptr < PLIP_MAX)
  1199. parport[parport_ptr++] = n;
  1200. else
  1201. printk(KERN_INFO "plip: too many ports, %s ignored.\n",
  1202. str);
  1203. } else if (!strcmp(str, "timid")) {
  1204. timid = 1;
  1205. } else {
  1206. if (ints[0] == 0 || ints[1] == 0) {
  1207. /* disable driver on "plip=" or "plip=0" */
  1208. parport[0] = -2;
  1209. } else {
  1210. printk(KERN_WARNING "warning: 'plip=0x%x' ignored\n",
  1211. ints[1]);
  1212. }
  1213. }
  1214. return 1;
  1215. }
  1216. __setup("plip=", plip_setup);
  1217. #endif /* !MODULE */
  1218. static int __init plip_init (void)
  1219. {
  1220. if (parport[0] == -2)
  1221. return 0;
  1222. if (parport[0] != -1 && timid) {
  1223. printk(KERN_WARNING "plip: warning, ignoring `timid' since specific ports given.\n");
  1224. timid = 0;
  1225. }
  1226. if (parport_register_driver (&plip_driver)) {
  1227. printk (KERN_WARNING "plip: couldn't register driver\n");
  1228. return 1;
  1229. }
  1230. return 0;
  1231. }
  1232. module_init(plip_init);
  1233. module_exit(plip_cleanup_module);
  1234. MODULE_LICENSE("GPL");
  1235. /*
  1236. * Local variables:
  1237. * compile-command: "gcc -DMODULE -DMODVERSIONS -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -g -fomit-frame-pointer -pipe -c plip.c"
  1238. * End:
  1239. */