@@ -77,6 +77,10 @@ static volatile unsigned char *via;
7777#define ST_ODD 0x20 /* ADB state: odd data byte */
7878#define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
7979
80+ /* ADB command byte structure */
81+ #define ADDR_MASK 0xF0
82+ #define CMD_MASK 0x0F
83+
8084static int macii_init_via (void );
8185static void macii_start (void );
8286static irqreturn_t macii_interrupt (int irq , void * arg );
@@ -117,7 +121,8 @@ static int reply_len; /* number of bytes received in reply_buf or req->reply */
117121static int status ; /* VIA's ADB status bits captured upon interrupt */
118122static int last_status ; /* status bits as at previous interrupt */
119123static int srq_asserted ; /* have to poll for the device that asserted it */
120- static int command_byte ; /* the most recent command byte transmitted */
124+ static u8 last_cmd ; /* the most recent command byte transmitted */
125+ static u8 last_poll_cmd ; /* the most recent Talk R0 command byte transmitted */
121126static int autopoll_devs ; /* bits set are device addresses to be polled */
122127
123128/* Check for MacII style ADB */
@@ -179,35 +184,49 @@ static int macii_init_via(void)
179184/* Send an ADB poll (Talk Register 0 command prepended to the request queue) */
180185static void macii_queue_poll (void )
181186{
182- /* No point polling the active device as it will never assert SRQ, so
183- * poll the next device in the autopoll list. This could leave us
184- * stuck in a polling loop if an unprobed device is asserting SRQ.
185- * In theory, that could only happen if a device was plugged in after
186- * probing started. Unplugging it again will break the cycle.
187- * (Simply polling the next higher device often ends up polling almost
188- * every device (after wrapping around), which takes too long.)
189- */
190- int device_mask ;
191- int next_device ;
192187 static struct adb_request req ;
188+ unsigned char poll_command ;
189+ unsigned int poll_addr ;
193190
191+ /* This only polls devices in the autopoll list, which assumes that
192+ * unprobed devices never assert SRQ. That could happen if a device was
193+ * plugged in after the adb bus scan. Unplugging it again will resolve
194+ * the problem. This behaviour is similar to MacOS.
195+ */
194196 if (!autopoll_devs )
195197 return ;
196198
197- device_mask = (1 << (((command_byte & 0xF0 ) >> 4 ) + 1 )) - 1 ;
198- if (autopoll_devs & ~device_mask )
199- next_device = ffs (autopoll_devs & ~device_mask ) - 1 ;
200- else
201- next_device = ffs (autopoll_devs ) - 1 ;
199+ /* The device most recently polled may not be the best device to poll
200+ * right now. Some other device(s) may have signalled SRQ (the active
201+ * device won't do that). Or the autopoll list may have been changed.
202+ * Try polling the next higher address.
203+ */
204+ poll_addr = (last_poll_cmd & ADDR_MASK ) >> 4 ;
205+ if ((srq_asserted && last_cmd == last_poll_cmd ) ||
206+ !(autopoll_devs & (1 << poll_addr ))) {
207+ unsigned int higher_devs ;
208+
209+ higher_devs = autopoll_devs & - (1 << (poll_addr + 1 ));
210+ poll_addr = ffs (higher_devs ? higher_devs : autopoll_devs ) - 1 ;
211+ }
202212
203- adb_request (& req , NULL , ADBREQ_NOSEND , 1 , ADB_READREG (next_device , 0 ));
213+ /* Send a Talk Register 0 command */
214+ poll_command = ADB_READREG (poll_addr , 0 );
215+
216+ /* No need to repeat this Talk command. The transceiver will do that
217+ * as long as it is idle.
218+ */
219+ if (poll_command == last_cmd )
220+ return ;
221+
222+ adb_request (& req , NULL , ADBREQ_NOSEND , 1 , poll_command );
204223
205224 req .sent = 0 ;
206225 req .complete = 0 ;
207226 req .reply_len = 0 ;
208227 req .next = current_req ;
209228
210- if (current_req != NULL ) {
229+ if (WARN_ON ( current_req ) ) {
211230 current_req = & req ;
212231 } else {
213232 current_req = & req ;
@@ -266,37 +285,22 @@ static int macii_write(struct adb_request *req)
266285/* Start auto-polling */
267286static int macii_autopoll (int devs )
268287{
269- static struct adb_request req ;
270288 unsigned long flags ;
271- int err = 0 ;
272289
273290 local_irq_save (flags );
274291
275292 /* bit 1 == device 1, and so on. */
276293 autopoll_devs = devs & 0xFFFE ;
277294
278- if (autopoll_devs && !current_req ) {
279- /* Send a Talk Reg 0. The controller will repeatedly transmit
280- * this as long as it is idle.
281- */
282- adb_request (& req , NULL , ADBREQ_NOSEND , 1 ,
283- ADB_READREG (ffs (autopoll_devs ) - 1 , 0 ));
284- err = macii_write (& req );
295+ if (!current_req ) {
296+ macii_queue_poll ();
297+ if (current_req && macii_state == idle )
298+ macii_start ();
285299 }
286300
287301 local_irq_restore (flags );
288- return err ;
289- }
290302
291- static inline int need_autopoll (void )
292- {
293- /* Was the last command Talk Reg 0
294- * and is the target on the autopoll list?
295- */
296- if ((command_byte & 0x0F ) == 0x0C &&
297- ((1 << ((command_byte & 0xF0 ) >> 4 )) & autopoll_devs ))
298- return 0 ;
299- return 1 ;
303+ return 0 ;
300304}
301305
302306/* Prod the chip without interrupts */
@@ -333,7 +337,12 @@ static void macii_start(void)
333337 */
334338
335339 /* store command byte */
336- command_byte = req -> data [1 ];
340+ last_cmd = req -> data [1 ];
341+
342+ /* If this is a Talk Register 0 command, store the command byte */
343+ if ((last_cmd & CMD_MASK ) == ADB_READREG (0 , 0 ))
344+ last_poll_cmd = last_cmd ;
345+
337346 /* Output mode */
338347 via [ACR ] |= SR_OUT ;
339348 /* Load data */
@@ -424,10 +433,11 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
424433 if (req -> done )
425434 (* req -> done )(req );
426435
427- if (current_req )
436+ if (!current_req )
437+ macii_queue_poll ();
438+
439+ if (current_req && macii_state == idle )
428440 macii_start ();
429- else if (need_autopoll ())
430- macii_autopoll (autopoll_devs );
431441 }
432442
433443 if (macii_state == idle ) {
@@ -507,14 +517,11 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
507517
508518 macii_state = idle ;
509519
510- /* SRQ seen before, initiate poll now */
511- if (srq_asserted )
520+ if (!current_req )
512521 macii_queue_poll ();
513522
514523 if (current_req )
515524 macii_start ();
516- else if (need_autopoll ())
517- macii_autopoll (autopoll_devs );
518525
519526 if (macii_state == idle )
520527 via [B ] = (via [B ] & ~ST_MASK ) | ST_IDLE ;
0 commit comments